
Quero criar um exercise
ambiente que possa ser usado em qualquer seção, subseção, etc. Como ilustração, considere o seguinte:
- quando na seção 1, o terceiro exemplo deve ser intitulado comoExemplo 1.3, mas
- quando na seção 1 e subseção 2, o terceiro exemplo deve ser intitulado comoExemplo 1.2.3.
Como fazer isso?
\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}
Fiz o seguinte, mas a saída tem subseção dupla quando estou na subseção.
\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}
Observação:
Sugestões de melhores práticas são bem-vindas para tornar o ambiente cada vez mais sofisticado, podendo ser referenciado por rótulo, etc, etc...
Responder1
Você deseja colocar o prefixo \theexercise
para que seja visto \ref
não apenas no 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}
Responder2
Refinamento paraA solução de David Carlisle, é muito longo para um comentário:
\newcounter{exercise}[subsection]
\makeatletter
\@addtoreset{exercise}{section}
\makeatother
\renewcommand\theexercise{%
\ifnum\value{subsection}>0 %
\thesubsection
\else
\thesection
\fi
.\arabic{exercise}%
}
Observações:
\newcounter{exercise}[subsection]
apenas redefine o contador para new\subsections
, não para\section
. É necessário um adicional\@addtoreset{exercise}{section}
.Dentro
\theexercise
eu usei\thesubsection
em vez de\arabic{subsection}
, então uma mudança\thesubsection
é automaticamente refletida em\theexercise
, por exemplo, se alguém quiser ter um estilo de numeração diferente para subseções do que o padrão.
Responder3
Acho que você pode comparar o contador da subseção com zero (por exemplo, usando comandos do etoolbox
) e usar isso para decidir como compor o número.