如何在进位计数器时抑制输出?

如何在进位计数器时抑制输出?

我将一本学习书分成多本单独的书。为了跨卷继续章节和部分编号,我使用了 Werner 在此处提供的解决方案:将计数器转移到另一个文件

太棒了!但有一个问题:存储计数器的命令会在文档中生成输出。该命令适用于我的情况,但在保存计数器时会产生额外的输出。我的问题是:如何消除额外的输出?

MWE(它会给出“缺失数字,视为零”的错误,但如果你按回车键继续,则会生成 PDF):

\documentclass[a4paper,10pt,fleqn,openany]{book}

% ---------------------------------------------------------------------------
% load chapter/part counters from previous workbooks 
% https://tex.stackexchange.com/questions/129312/carrying-counters-over-to-another-file
% ===========================================================================
\usepackage{refcount,xr}
\externaldocument{book1}
\externaldocument{book3}
% ---------------------------

% command for storing part/chapter counters
% ---------------------------
\makeatletter
\newcommand*{\storecounter}[2]{%
  % updated as per egreg's comment
  \edef\@currentlabel{\the\value{#1}}% Store current counter value in \@currentlabel
  \label{#2}% Store label
}
\makeatother

% command to get value of counters
% ---------------------------
\newcommand*{\getcounter}[2]{%
  \setcounterref{#1}{#2}% Retrieve label value and store it in a counter
}

% have a counter for chapters and for parts from volume 1
% ---------------------------
\newcounter{bookonecounter}
\getcounter{bookonecounter}{parts}
\setcounter{part}{\thebookonecounter}

\begin{document}

\part{A part}
\part{Another part}

% ===========================================================================
% Save chapter/part counters for use in further volumes
% XXX: this currently outputs "value in X"
% ---------------------------
\setcounter{bookonecounter}{\thechapter}\storecounter{bookonecounter}{chapters}
\setcounter{bookonecounter}{\thepart}\storecounter{bookonecounter}{parts}
% ===========================================================================

\end{document}

当卷 1 为“book1.tex”时,使用此示例(卷 2)将正确继续部分编号。例如,对于包含以下内容的 book1.aux

\newlabel{chapters}{{4}{154}}
\newlabel{parts}{{2}{154}}

它会编译(如上所述的投诉)为继续计数的 PDF。但是,它会在 PDF 末尾添加一页,其中包含IVMWE 的输出:带有“IV”的额外页面

注释掉这两setcounter/storecounter行会将其从 pdf 中删除。

编辑:egreg 的提议确实有所改进。屏幕截图是使用他的定义的结果。不幸的是,由于线条的原因,仍然有一个多余的、不需要的页面,上面有一些文本\setcounter..\storecounter

关于如何防止这些行\setcounter{}..\storecounter产生额外的页面,您有什么想法吗?

答案1

这里存在一些问题:如果用作计数器输出格式,则将\setcounter{bookonecounter}{\thepart}不起作用,即,等——这不能被理解为计数器值的有效输入。\thepart\Roman{...}IIIIII\setcounter

\setcounter{foo}{\thefoobar}所以在任何情况下都不要使用!

\documentclass[a4paper,10pt,fleqn,openany]{book}

\begin{filecontents}{book1.tex}
\documentclass{book}

\renewcommand{\thepart}{\arabic{part}}
\begin{document}
\part{First part}\label{parts}

\chapter{First chapter}

\end{document}
\end{filecontents}


% ---------------------------------------------------------------------------
% load chapter/part counters from previous workbooks 
% http://tex.stackexchange.com/questions/129312/carrying-counters-over-to-another-file
% ===========================================================================
\usepackage{refcount,xr}
\externaldocument{book1}
\externaldocument{book3}
% ---------------------------

% command for storing part/chapter counters
% ---------------------------
\makeatletter
\newcommand*{\storecounter}[2]{%
  \edef\@currentlabel{\csname the#1\endcsname}% Store current counter value in \@currentlabel
  \label{#2}% Store label
}
\makeatother

% command to get value of counters
% ---------------------------
\newcommand*{\getcounter}[2]{%
  \setcounterref{#1}{#2}% Retrieve label value and store it in a counter
}

% have a counter for chapters and for parts from volume 1
% ---------------------------
\newcounter{bookonecounter}
\getcounter{bookonecounter}{parts}
\setcounter{part}{\value{bookonecounter}}

\begin{document}

\part{A part}
\part{Another part}

% ===========================================================================
% Save chapter/part counters for use in further volumes
% XXX: this currently outputs "value in X"
% ---------------------------
\setcounter{bookonecounter}{\value{chapter}}
\storecounter{bookonecounter}{chapters}
\setcounter{bookonecounter}{\value{part}}
\storecounter{bookonecounter}{parts}
% ===========================================================================

\end{document}

相关内容