
次のコードを使用して、警告記号、警告という単語、および内容テキストを含むボックスを描画します。ここでのタスクは、警告に番号を付けることです。これを印刷するにはどうすればよいでしょうか。たとえば、警告が第 1 章に表示された場合、警告 1.1 と呼ばれます。次の警告は 1.2 と呼ばれます。
\newcounter{myWarning}
\newcommand{\myWarning}[1]
{
\refstepcounter{myWarning}
\begin{longtable}[H]{|p{0.1\linewidth}m{0.9\linewidth}|}\hline
\textbf{WARNING} & \\
\includegraphics[width=1cm]{"CommonSubdocuments/Pictures/WarningSign"} & \textbf{#1} \\\hline
\end{longtable}
\addtocounter{table}{-1}
}
答え1
カウンタ(たとえば foo)が で定義されている場合\newcounter
、 が自動的に定義され、\thefoo
デフォルトで になります\arabic{foo}
。つまり、カウンタの値がアラビア数字で出力されます。
\newcounter{foo}[chapter]
新しい章が始まるたびにカウンターをリセットします (より適切な方法は、chapter
カウンターが\refstepcounter
または増加したときです\stepcounter
)。
番号付けの形式を変更するには、 を使用します\renewcommand{\thefoo}{\thechapter.\arabic{foo}
。つまり、foo 番号の前に章番号が付きます。
これは同様のアプローチで、 とtcolorbox
を使用して とnumber within
で自動的に実行されますuse counter=...
。
\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage{bclogo}
\newcounter{myWarning}[chapter]
%\renewcommand{\themyWarning}{\thechapter.\arabic{myWarning}}
\newtcolorbox[use counter=myWarning,number within=chapter]{warningbox}[1][]{enhanced jigsaw, sharp corners,title={Warning \thetcbcounter},#1}
\newcommand{\myWarning}[1][]{%
\begin{warningbox}{#1}
\textbf{WARNING}
\bcattention% Warning sign
\end{warningbox}
}
\begin{document}
\chapter{Foo}
\myWarning
\myWarning
\chapter{Foobar}
\myWarning
\end{document}