如果计数器仅显示一次则隐藏它

如果计数器仅显示一次则隐藏它

我的文档上有两个宏。\showcounter增加计数器,然后显示它。\resetcounter将计数器设置回“0”。

\documentclass{article}
\newcounter{counterone}
\setcounter{counterone}{0}
\newcommand{\showcounter}{
    \stepcounter{counterone}
    \arabic{counterone}
}
\newcommand{\resetcounter}{
\setcounter{counterone}{0}
}
\begin{document}
    \showcounter \showcounter \showcounter %A
    \resetcounter
    \showcounter \showcounter %B
    \resetcounter
    \showcounter %C
    \resetcounter
\end{document}

这将创建一个计数器,该计数器在被使用之前一直增加。但是,在被使用之前只使用过一次的\resetcounter情况下,我不希望显示任何值。\showcounter\resetcounter

  • 在这种情况下我该如何隐藏计数器?

更新,澄清一下,输出应该像这样:

(A)1、2、3

(B)1、2

(C)

在最后一种情况下,没有出现“1”,因为直到下一次才再次使用计数器resetcounter

答案1

现在(从你问题的第一个版本开始)我假设以下解决方案可能会超出你的 TeX 的容量(也许可以尝试lualatex一下?),但我想我无法对其进行进一步优化。aux文件中会创建一个条目仅有的当实际重置计数器为 1 时;此条目定义了一个空宏,它是向\show... 不是显示其参数。(这可能可以进行内存优化,这样我们就不用创建所有这些宏,而是创建一个包含数字列表的巨大宏。这很可能会节省内存,但会大大增加编译时间。亲爱的巫师:请确认我在这里是否正确;)。)可选参数给出\show...一个宏(如\alph\arabic),它实际上显示给定的计数器;用户必须注意任何空格。

当然,您需要两次。但是,日志文件/终端中没有显示此信息(例如“再次运行 LaTeX”)。

现在我非常乐意将此解决方案变成包;我想象的一个用例是,当论文中只有一个定理时,不要对定理进行编号。因此,\defineoneshotcounter欢迎提出有关用户界面(包括现在愚蠢地称为宏的更好的名称)等的任何建议。

注意:请参阅下面的 Bruno Le Floch 的评论,以获得一个很好的解决方案,其中\counteronestyle是默认值而不是arabic(这里没有放,以免过多地破坏代码)。

\documentclass{article}

\makeatletter

\newcommand{\defineoneshotcounter}[1]{%
  \newcounter{#1}
  \newcounter{#1@helper}
  \expandafter\newcommand\csname show#1\endcsname[1][\arabic]{%
    \stepcounter{#1}%
    \stepcounter{#1@helper}%
    \@ifundefined{#1@reset@\the\csname c@#1@helper\endcsname}{##1{#1}}{}%
  }
  \@namedef{reset#1}{%
    \ifnum\csname c@#1\endcsname=1\if@filesw
      \immediate\write\@auxout{%
        \string\global\string\@namedef{#1@reset@\the\csname c@#1@helper\endcsname}{}%
      }%
    \fi\fi
    \setcounter{#1}{0}%
  }
}

\makeatother

\defineoneshotcounter{counterone}

\newcommand{\counteronestyle}[1]{\{\arabic{#1}\}}

\begin{document}
    \showcounterone \showcounterone \showcounterone
    \resetcounterone

    \showcounterone[\alph]\showcounterone[\alph]
    \resetcounterone

    \showcounterone
    \resetcounterone

    \showcounterone[\counteronestyle]\showcounterone[\counteronestyle]%
    \showcounterone[\counteronestyle]\showcounterone[\counteronestyle]
    \resetcounterone

    \showcounterone
    \resetcounterone

    \showcounterone \showcounterone
    \resetcounterone
\end{document}

答案2

\begin{filecontents*}{disccounters.sty}
\RequirePackage{keyval2e}
\long\def\gobble@to@relax#1\cpt@relax{}
\kve@definekeys[DC]{disccount}[disc@]{%
  format/\@firstofone,
  numbertype/\arabic/
    \ifescapedTF{#1}{%
      \xifinsetTF{,\detokenize{#1},}{%
        ,\detokenize{\arabic,\alph,\Alph},%
      }{}{%
        \@latex@error{Invalid number type '\detokenize{#1}'}\@ehc
      }%
    }{%
      \@latex@error{Number type '\detokenize{#1}' must be escaped}\@ehc
    },
}
\newcounter{disccounter@aux}
\BeforeStartOfDocument{%
  \ifdefTF\disccounter@hook{}{%
    \def\disccounter@hook{}%
  }%
}
\newcommand*\discc@unter@aux{\the\numexpr\thedisccounter@aux-20000\relax}
\robust@def*\DefineDiscCounters{\cpt@testopt\disc@definecounters{}}
\robust@def*\disc@definecounters[#1]#2{%
  \cptexpandargonce{\kve@setafterdefaults[DC]{disccount}}{#1}%
  \setcounter{disccounter@aux}{0}%
  \docommalist{#2}{%
    \newcounter{##1}%
    \expandafter\newcommand\csname show##1\endcsname[1][]{%
      \stepcounter{##1}%
      \stepcounter{disccounter@aux}%
      \cptexpandargonce{\kve@setafterdefaults[DC]{disccount}}{####1}%
      \xifinsetTF{,##1@reset@\discc@unter@aux,}%
        {,\disccounter@hook,}{}{\disc@format{\disc@numbertype{##1}}}%
    }%
    \csn@def{reset##1}{%
      \ifnum\csname c@##1\endcsname=\@ne
        \xifinsetTF{,##1@reset@\discc@unter@aux,}{,\disccounter@hook,}{}{%
          \xdef\disccounter@hook{%
            \ifcsemptyTF\disccounter@hook{}{\disccounter@hook,}%
            ##1@reset@\discc@unter@aux
          }%
        }%
      \fi
      \setcounter{##1}{0}%
    }%
  }%
}
\AtEndDocument{%
  \if@filesw
    \immediate\write\@auxout{%
      \gdef\string\disccounter@hook{\disccounter@hook}%
    }%
  \else
    \@latex@warning@no@line{File '\jobname.aux' not open}%
  \fi
}
\newcommand*\discbraced[1]{{\tt\string{#1\string}}}
\endinput
\end{filecontents*}

\documentclass{article}
\usepackage{disccounters}
\usepackage{color}
\DefineDiscCounters[numbertype=\arabic]{counterone,countertwo,counterthree}

\begin{document}
\begin{enumerate}
% If you hate the optional argument, you can use the plain \show... 
% (without argument), or put the optional arguments in a command (see later).
\item \showcounterone \showcounterone \showcounterone
  \resetcounterone
\item \showcounterone[numbertype=\alph]\showcounterone[numbertype=\alph]%
  \resetcounterone
\item \showcounterone
  \resetcounterone
\item \showcounterone[numbertype=\arabic]\showcounterone[numbertype=\arabic]%
  \showcounterone[numbertype=\arabic]\showcounterone[numbertype=\arabic]%
  \resetcounterone
\item \showcounterone
  \resetcounterone
\item \showcounterone[numbertype=\Alph]\showcounterone[numbertype=\Alph]
  \resetcounterone
\end{enumerate}

\begingroup
\color{red}
\begin{enumerate}
\item \showcountertwo \showcountertwo \showcountertwo
  \resetcountertwo
\item \showcountertwo[numbertype=\alph]\showcountertwo[numbertype=\alph]%
  \resetcountertwo
\item \showcountertwo
  \resetcountertwo
\item \showcountertwo[numbertype=\arabic]\showcountertwo[numbertype=\arabic]%
  \showcountertwo[numbertype=\arabic]\showcountertwo[numbertype=\arabic]%
  \resetcountertwo
\item \showcountertwo
  \resetcountertwo
\item \showcountertwo \showcountertwo
  \resetcountertwo
\end{enumerate}
\endgroup

\begingroup
\color{blue}
\def\tempa{numbertype=\alph,format=\discbraced}
\def\tempb{numbertype=\arabic,format=\discbraced}
\begin{enumerate}
\item \showcounterthree \showcounterthree \showcounterthree
  \resetcounterthree
\item \showcounterthree[\tempa]%
  \showcounterthree[numbertype=\Alph,format=\discbraced]%
  \resetcounterthree
\item \showcounterthree
  \resetcounterthree
\item \showcounterthree[\tempb]\showcounterthree[\tempb]%
  \showcounterthree[\tempb]\showcounterthree[\tempb]%
  \resetcounterthree
\item \showcounterthree
  \resetcounterthree
\item \showcounterthree \showcounterthree
  \resetcounterthree
\end{enumerate}
\endgroup

\end{document}

在此处输入图片描述

相关内容