섹션 또는 하위 섹션 내부에 있는지 여부에 따라 카운터를 조정하는 방법

섹션 또는 하위 섹션 내부에 있는지 여부에 따라 카운터를 조정하는 방법

섹션, 하위 섹션 등에서 사용할 수 있는 환경을 만들고 싶습니다. exercise예를 들어 다음을 고려하십시오.

  • 섹션 1에서 세 번째 예의 제목은 다음과 같아야 합니다.예제 1.3, 하지만
  • 섹션 1과 하위 섹션 2에서 세 번째 예의 제목은 다음과 같아야 합니다.예 1.2.3.

어떻게 해야 하나요?

여기에 이미지 설명을 입력하세요

\documentclass{article}
\usepackage{tcolorbox}

\newcounter{exercise}
\newenvironment{exercise}
{\par\smallskip\refstepcounter{exercise}\begin{tcolorbox}[title=Exercise \thesection.\thesubsection.\theexercise]\ignorespaces}
{\end{tcolorbox}\par\smallskip\ignorespacesafterend}

\begin{document}
\section{Polynomial}
In this section we will study expressions in the following form.
\[
a_0 x^n + a_1 x^{n-1} \cdots + a_n
\]
where \ldots
\begin{exercise}
Is $\frac{x-1}{2}$ a polynomial?
\tcblower
Yes. Because it can be rewritten as $x/2-1/2$.
\end{exercise}
\begin{exercise}
Is $0$ a polynomial?
\tcblower
Yes.
\end{exercise}
\subsection{Order}
The highest exponent in polynomial terms represents the order of  the polynomial.
\begin{exercise}
What is the order of $2x^3-3x^5+1$?
\tcblower
The highest exponent is 5 so the polynomial order is 5.
\end{exercise}
\end{document}

다음과 같이 했지만 하위 섹션에 있을 때 출력에 이중 하위 섹션이 있습니다.

\newcounter{exercise}
\newenvironment{exercise}
{\par\smallskip\refstepcounter{exercise}\begin{tcolorbox}[title=Exercise \thesection\ifnum\value{subsection}>0.\thesubsection\fi.\theexercise]\ignorespaces}
{\end{tcolorbox}\par\smallskip\ignorespacesafterend}

메모:

라벨 등으로 참조할 수 있는 등 환경을 더욱 정교하게 만들기 위한 모범 사례 제안을 환영합니다.

답변1

여기에 이미지 설명을 입력하세요

제목뿐만 아니라 \theexercise눈에 띄도록 접두사를 넣고 싶습니다 .\ref

\documentclass{article}
\usepackage{tcolorbox}

\newcounter{exercise}[subsection]
\renewcommand\theexercise{%
\thesection.%
\ifnum\value{subsection}>0 \arabic{subsection}.\fi
\arabic{exercise}}

\newenvironment{exercise}
{\par\smallskip\refstepcounter{exercise}
\begin{tcolorbox}[title=Exercise \theexercise]\ignorespaces}
{\end{tcolorbox}\par\smallskip\ignorespacesafterend}

\begin{document}
\section{Polynomial}
In this section we will study expressions in the following form.
\[
a_0 x^n + a_1 x^{n-1} \cdots + a_n
\]
where \ldots
\begin{exercise}
Is $\frac{x-1}{2}$ a polynomial?
\tcblower
Yes. Because it can be rewritten as $x/2-1/2$.
\end{exercise}
\begin{exercise}
Is $0$ a polynomial?
\tcblower
Yes.
\end{exercise}
\subsection{Order}
The highest exponent in polynomial terms represents the order of  the polynomial.
\begin{exercise}
What is the order of $2x^3-3x^5+1$?
\tcblower
The highest exponent is 5 so the polynomial order is 5.
\end{exercise}
\end{document}

답변2

개선 대상David Carlisle의 솔루션, 댓글이 너무 깁니다.

\newcounter{exercise}[subsection]
\makeatletter
\@addtoreset{exercise}{section}
\makeatother

\renewcommand\theexercise{%
  \ifnum\value{subsection}>0 %
    \thesubsection
  \else
    \thesection
  \fi
  .\arabic{exercise}%
}

비고:

  • \newcounter{exercise}[subsection]\subsections는 카운터 가 아닌 new 에 대해서만 카운터를 재설정합니다 \section. 추가 \@addtoreset{exercise}{section}가 필요합니다.

  • 내부에서는 을 대신 \theexercise사용했으며 , 변경 사항은 에 자동으로 반영됩니다 . 예를 들어 누군가가 하위 섹션에 대해 기본값과 다른 번호 매기기 스타일을 원하는 경우입니다.\thesubsection\arabic{subsection}\thesubsection\theexercise

답변3

내 생각에는 하위 섹션 카운터를 0과 비교하고(예: 의 명령 사용 etoolbox) 이를 사용하여 숫자를 구성하는 방법을 결정할 수 있다고 생각합니다.

관련 정보