
セクション、サブセクションなどいずれでも使用できる環境を作成したいと考えています。exercise
例として、次のことを考慮してください。
- セクション1では、3番目の例のタイトルは次のようにします。例1.3、 しかし
- セクション1とサブセクション2の場合、3番目の例は次のようにタイトルを付ける必要があります。例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}
次のようにしましたが、サブセクションにいるときに出力にサブセクションが 2 つ表示されます。
\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
改良デビッド・カーライルの解決策コメントとしては長すぎます:
\newcounter{exercise}[subsection]
\makeatletter
\@addtoreset{exercise}{section}
\makeatother
\renewcommand\theexercise{%
\ifnum\value{subsection}>0 %
\thesubsection
\else
\thesection
\fi
.\arabic{exercise}%
}
備考:
\newcounter{exercise}[subsection]
\subsections
は、新しい のカウンターのみをリセットし、 のカウンターはリセットしません\section
。 を追加する\@addtoreset{exercise}{section}
必要があります。内部ではの代わりに
\theexercise
を使用しています。 の変更は に自動的に反映されます。たとえば、サブセクションの番号付けスタイルをデフォルトとは異なるものにしたい場合などです。\thesubsection
\arabic{subsection}
\thesubsection
\theexercise
答え3
etoolbox
サブセクション カウンターをゼロと比較し (たとえば、 からのコマンドを使用して)、これを使用して数値の構成方法を決定できると思います。