
Quiero crear un exercise
entorno 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?
\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
Desea colocar el prefijo \theexercise
para que no solo lo vea \ref
el 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
\theexercise
he usado\thesubsection
en lugar de\arabic{subsection}
, luego un cambio\thesubsection
se 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.