如何為我自己的命令添加一個也列印的計數器?

如何為我自己的命令添加一個也列印的計數器?

我使用以下程式碼繪製一個帶有警告標誌、警告一詞和內容文字的方塊。現在的任務是計算警告。我怎樣才能列印這個?例如,如果我的警告出現在第 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 編號前面帶有章節編號。

這是一個類似的方法,這是透過 atcolorbox和 使用number withinand自動完成的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}

在此輸入影像描述

相關內容