查找待办事项的数量(来自上次运行)

查找待办事项的数量(来自上次运行)

我希望有一个小的“\if”有待办事项;然后在第一页显示一个大警告和待办事项列表。否则不显示。

我可以毫无困难地访问 todonotes 计数器:

\documentclass[11pt]{book}
\usepackage[english]{babel}
\usepackage{todonotes}

\begin{document}
%\ifthenelse{ someCounter > 0}{Warning - todos left! \listoftodos\pagebreak}{}

\listoftodos

It is initially to 0=\arabic{@todonotes@numberoftodonotes}

Hello \todo{One thing to do} World\todo{A second things to do}.

And it is now  2=\arabic{@todonotes@numberoftodonotes}
\end{document}  

但是每次运行时 @todonotes@numberoftodonotes 都会重置为 0。所以我想找到 listoftodos 的计数——第二次运行时会填充该计数。

如何最好地做到这一点?

答案1

\contentsline您可以计算中的调用次数\listoftodos,这样您就知道待办事项的数量,但仅限于之后\listoftodos

\documentclass[11pt]{book}
\usepackage[english]{babel}
\usepackage{todonotes}
\usepackage{xpatch}

\newcounter{totalnumberoftodos}

\newcommand\patchcontentsline
  {%
    \xpretocmd\contentsline{\stepcounter{totalnumberoftodos}}{}{}%
  }
\xpretocmd\listoftodos{\begingroup\patchcontentsline}{}{}
\xapptocmd\listoftodos{\endgroup}{}{}

\begin{document}
%\ifthenelse{ someCounter > 0}{Warning - todos left! \listoftodos\pagebreak}{}

\listoftodos

The total number of todo-notes was \arabic{totalnumberoftodos}.

It is initially to 0=\arabic{@todonotes@numberoftodonotes}

Hello \todo{One thing to do} World\todo{A second things to do}.

And it is now  2=\arabic{@todonotes@numberoftodonotes}

\end{document}  

在此处输入图片描述

编辑:不需要\listoftodos使用的版本:

\documentclass[11pt]{book}
\usepackage[english]{babel}
\usepackage{todonotes}

\makeatletter
\AtEndDocument
  {%
    \immediate\write\@auxout
      {\gdef\noexpand\totaltodos{\the\value{@todonotes@numberoftodonotes}}}%
  }
\newcommand*\iftodos
  {%
    \@ifundefined{totaltodos}
      {}
      {%
        \ifnum\totaltodos>0
          \expandafter\@secondoftwo
        \fi
        \@gobble
      }%
  }
\@ifundefined{totaltodos}{\def\totaltodos{0}}{}
\makeatother

\begin{document}
\iftodos
  {%
    Warning - todos left!
    \listoftodos\newpage
  }

The total number of todo-notes was \totaltodos.

It is initially to 0=\arabic{@todonotes@numberoftodonotes}

Hello \todo{One thing to do} World\todo{A second things to do}.

And it is now  2=\arabic{@todonotes@numberoftodonotes}

\end{document}

答案2

这个包裹totcount正是您要找的东西。

\documentclass[11pt]{book}
\usepackage[english]{babel}
\usepackage{todonotes}
\usepackage{totcount}

\regtotcounter{@todonotes@numberoftodonotes}
\AtBeginDocument{%
  \ifnum\totvalue{@todonotes@numberoftodonotes}>0
    Warning --- todos left! \listoftodos\clearpage
  \fi
}

\begin{document}

It is initially to 0=\arabic{@todonotes@numberoftodonotes}

Hello \todo{One thing to do} World\todo{A second things to do}.

And it is now  2=\arabic{@todonotes@numberoftodonotes}

\end{document}

确保运行 LaTeX 两次。

相关内容