如何在回忆录类的第一页打印最后一个 \refstepcounter 的值?

如何在回忆录类的第一页打印最后一个 \refstepcounter 的值?

我创建了一个名为 的环境sol,它有一个\refstepcounter命令。我想在文档的第一页上打印sol我在文档中创建了多少个环境。

\documentclass[extrafontsizes,11pt,twocolumn,openany,openbib]{memoir}

\usepackage{makeidx}
\usepackage[T1]{fontenc}

\newcounter{sol}

\newenvironment{sol}[2]%
{\refstepcounter{sol}\index{\textbf{#2}}%
\addcontentsline{toc}{section}{\protect #1}%
\centering\textsc{\Large\textbf{#1}}\\%
\noindent By: \textbf{#2}\\%    
\begin{raggedright}\ignorespaces}%
{\end{raggedright}\ignorespacesafterend}

\begin{document}
This document consists of \thesol\ sols.
\tableofcontents
\twocolumn
.
.
.
\printindex
\end{document}

sol但是,由于命令没有前导sol环境,我得到的第一页计数器的值总是 0。\thesol因此我想出了以下解决方案:

\documentclass[extrafontsizes,11pt,twocolumn,openany,openbib]{memoir}

\usepackage{color}
\usepackage{makeidx}
\usepackage[T1]{fontenc}

\newcounter{sol}
\addtocounter{sol}{-1}

\newenvironment{sol}[2]%
{\refstepcounter{sol}\index{\textbf{#2}}%
\addcontentsline{toc}{section}{\protect #1}%
\centering\textsc{\Large\textbf{#1}}\\%
\noindent By: \textbf{#2}\\%    
\begin{raggedright}\ignorespaces}%
{\end{raggedright}\ignorespacesafterend}

\begin{document}
This document consists of \ref{sol:num}\ sols.
\tableofcontents
\twocolumn
.
.
.
{\color{white}
\begin{sol}{\color{white}a}{\color{white}a}
\label{sol:num}
\end{sol}}
\printindex
\end{document}

但是,出现的新问题是代码的输出\color{white}a出现在 ToC 和 Index 中,正如sol环境所指定的。 笔记:我把第二个问题放在了同一个主题中,因为根据我目前的知识,我认为第一个问题是无法解决的LaTeX

答案1

这个包totcount有以下用途:

\documentclass{memoir}
\usepackage{totcount}

\usepackage{lipsum} % just for the example

\newcounter{sol}
\regtotcounter{sol}

\newenvironment{sol}[2]
  {\refstepcounter{sol}\index{\textbf{#2}}%
   \addcontentsline{toc}{section}{#1}%
   \centering{\Large\scshape #1\\}
   \raggedright
   By: \textbf{#2}\\
   \ignorespaces}
  {\par}

\begin{document}
This document consists of \total{sol} sols.

\begin{sol}{Abc}{Def}
\lipsum*[2]
\end{sol}

Another

\begin{sol}{Abc}{Def}
\lipsum*[3]
\end{sol}

\end{document}

在此处输入图片描述

答案2

另一个解决方案:LastPage对另一个计数器使用该技巧:

sol将计数器的值写入.aux文档末尾的文件中并重新运行(基本上总是这样做)pdflatex(或latex) - 它读取存储在TotalSolEnvs并可以在需要的地方输出。

\AtEndDocument命令执行文档末尾的命令,因此该命令\WriteNumberOfSolEnvs也负责存储使用的解决方案环境的数量。

此机制也适用于其他计数器。

\documentclass[extrafontsizes,11pt,twocolumn,openany,openbib]{memoir}

\usepackage{color}
\usepackage{makeidx}
\usepackage[T1]{fontenc}

\newcounter{sol}

\newcounter{TotalSolEnvs}%

\makeatletter
\newcommand{\WriteNumberOfSolEnvs}{%
\immediate\write\@auxout{%
  \string\setcounter{TotalSolEnvs}{\number\value{sol}}
}
}
\makeatother


\AtEndDocument{%
  \WriteNumberOfSolEnvs%
}%

\newenvironment{sol}[2]%
{\refstepcounter{sol}\index{\textbf{#2}}%
\addcontentsline{toc}{section}{\protect #1}%
\centering\textsc{\Large\textbf{#1}}\\%
\noindent By: \textbf{#2}\\%    
\begin{raggedright}\ignorespaces}%
{\end{raggedright}\ignorespacesafterend}

\begin{document}
This document consists of \number\value{TotalSolEnvs}~ sols.
\tableofcontents
\twocolumn
\begin{sol}{Arg1}{Arg2}%
\end{sol}

\begin{sol}{Arg1}{Arg2}%
\end{sol}

\begin{sol}{Arg1}{Arg2}%

\end{sol}

\begin{sol}{Arg1}{Arg2}%
\end{sol}


\printindex
\end{document}

在此处输入图片描述

答案3

将此添加到您的序言中:

\usepackage{refcount}
\newcommand{\manuallabel}[2]{\def\@currentlabel{#2}\label{#1}}
\newcounter{sol}
\addtocounter{sol}{0}

\newenvironment{sol}[2]%
{\refstepcounter{sol}\index{\textbf{#2}}%
\addcontentsline{toc}{section}{\protect #1}%
\centering\textsc{\Large\textbf{#1}}\\%
\noindent By: \textbf{#2}\\%    
}

并将其添加到文档末尾:

\manuallabel{counter}{\number\numexpr\getrefnumber{solref@\number\value{sol}}}

现在您可以使用

This document consists of \ref{counter} sols.

\sol{1}{2}

您可以通过更改以下两行来使用不同的名称:

\manuallabel{NAME}{...
This document consists of \ref{NAME} sols.

例子:

\documentclass[extrafontsizes,11pt,twocolumn,openany,openbib]{memoir}

\usepackage{color}
\usepackage{makeidx}
\usepackage[T1]{fontenc}

\usepackage{refcount}
\newcommand{\manuallabel}[2]{\def\@currentlabel{#2}\label{#1}}
\newcounter{sol}
\addtocounter{sol}{0}

\newenvironment{sol}[2]%
{\refstepcounter{sol}\index{\textbf{#2}}%
\addcontentsline{toc}{section}{\protect #1}%
\centering\textsc{\Large\textbf{#1}}\\%
\noindent By: \textbf{#2}\\%    
}

\begin{document}
This document consists of \ref{counter} sols.
\tableofcontents

\sol{1}{1}
\sol{1}{1}

\manuallabel{counter}{\number\numexpr\getrefnumber{solref@\number\value{sol}}}
\end{document}

本文件由 2 个 sol 组成。

答案4

\documentclass[extrafontsizes,11pt,twocolumn,openany,openbib]{memoir}

\usepackage{makeidx}
\usepackage[T1]{fontenc}

\makeatletter% I stole this, more or less, from hyperref
\def\total{%
  \@bsphack
  \begingroup
    \protected@write\@auxout{}{%
      \string\newlabel{total}{%
        {}% not used
        {\thepage}%
        {}% not used
        {\arabic{sol}}{}%
      }%
    }%
  \endgroup
  \@esphack
}%
\makeatother

\newcounter{sol}

\newenvironment{sol}[2]%
{\stepcounter{sol}\index{\textbf{#2}}%
\addcontentsline{toc}{section}{\protect #1}%
\centering\textsc{\Large\textbf{#1}}\\%
\noindent By: \textbf{#2}\\%    
\begin{raggedright}\ignorespaces}%
{\end{raggedright}\ignorespacesafterend}

\begin{document}
This document consists of \ref{total} sols.
\tableofcontents

\begin{sol}{First}{Golly}
Body of text.
\end{sol}

\begin{sol}{Second}{Gosh}
More text.
\end{sol}

\total
\printindex
\end{document}

相关内容