Cómo ajustar un contador dependiendo de si estamos o no dentro de una sección o subsección

Cómo ajustar un contador dependiendo de si estamos o no dentro de una sección o subsección

Quiero crear un exerciseentorno que pueda usarse en cualquiera de las secciones, subsecciones, etc. A modo de ilustración, considere lo siguiente:

  • cuando en la sección 1, el tercer ejemplo debe titularse comoEjemplo 1.3, pero
  • cuando en la sección 1 y la subsección 2, el tercer ejemplo debe titularse comoEjemplo 1.2.3.

¿Como hacer esto?

ingrese la descripción de la imagen aquí

\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}

Hice lo siguiente pero el resultado tiene doble subsección cuando estoy en la subsección.

\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}

Nota:

Se aceptan sugerencias de mejores prácticas para hacer que el entorno sea cada vez más sofisticado, como por ejemplo que se pueda hacer referencia a él mediante etiquetas, etc., etc.

Respuesta1

ingrese la descripción de la imagen aquí

Desea colocar el prefijo \theexercisepara que no solo lo vea \refel título.

\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}

Respuesta2

Refinamiento paraLa solución de David Carlisle, es demasiado largo para un comentario:

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

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

Observaciones:

  • \newcounter{exercise}[subsection]Sólo restablece el contador de nuevos \subsections, no de \section. \@addtoreset{exercise}{section}Se necesita un adicional .

  • En el interior \theexercisehe usado \thesubsectionen lugar de \arabic{subsection}, luego un cambio \thesubsectionse refleja automáticamente en \theexercise, por ejemplo, si alguien quiere tener un estilo de numeración para las subsecciones diferente al predeterminado.

Respuesta3

Creo que puedes comparar el contador de la subsección con cero (por ejemplo, usando comandos de etoolbox) y usarlo para decidir cómo componer el número.

información relacionada