如何分别提取每个部分的计数器值?

如何分别提取每个部分的计数器值?
\documentclass[a4paper, 12pt]{article}
%%% Main drive for pdflatex %%
\usepackage{amsmath,amssymb}
\usepackage{enumerate}
\usepackage[flushleft]{paralist}[2013/06/09]
\usepackage{pgffor}
% \foreach \n in {0,...,22}{do something}
\usepackage[nonumberofruns]{xassoccnt}
%% length setup
\setlength{\parindent}{0pt}
\newlength{\qspace}
\setlength{\qspace}{20pt}
%\newcounter{qnumber}
\NewTotalDocumentCounter{qnumber}
\setcounter{qnumber}{0}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newenvironment{question}%%%%%%%%%%%%%%%%%%%%%%%%%%%
{\vspace{\qspace}
%\clearpage
    \begin{enumerate}[\bfseries 1\quad][10]%
    \setcounter{enumi}{\value{qnumber}}%
    \item%
}{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \end{enumerate}
    \filbreak
    \stepcounter{qnumber}
}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\textsc{
In this paper:\\
There are 3 questions in section A. \\
There are 2 questions in section B. \\
There are 3 questions in section C. \\
There are \TotalValue{qnumber} questions in total.
}
\section*{Section A: \ \ \ Pure Mathematics}
\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}
\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}
\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}
\section*{Section B: \ \ \ XXXXXX}
\begin{question}
some texts some texts some texts some texts some texts 
\end{question}
\begin{question}
some texts some texts some texts some texts some texts 

\end{question}
\section*{Section C: \ \ \ XXXXX}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}
\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}
\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}

\end{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

有没有办法可以自动获取每个部分这三个值的值,这样我就不必手动计算了?

There are xxx questions in section A. \\
There are xxx questions in section B. \\
There are xxx questions in section C. \\

我希望做出一个全局性的改变,即在定义之前改变一些内容\开始{文档}所以我不必对已有的每个文件进行更改。

我还应该提到,我必须保持问题编号保持不变。也就是说,它们是不会从每个新的部分*{}重新开始。

谢谢。

答案1

您想要保存qnumber新部分开始时的值(以及文档结束时的值)。

如果您定义自己的命令,则会更容易,因此您还可以享受不需要每次手动格式化的好处。

\documentclass[a4paper, 12pt]{article}
\usepackage{amsmath,amssymb}
\usepackage{enumerate}
\usepackage[flushleft]{paralist}[2013/06/09]

%% infrastructure
\newcounter{qsection}
\renewcommand{\theqsection}{\Alph{qsection}}

\newcommand{\qsection}[1]{%
  % save the value of qnumber
  \saveqnumber
  \stepcounter{qsection}%
  \section*{Section \theqsection:\quad #1}%
}
\AtEndDocument{\saveqnumber}

\ExplSyntaxOn
\NewDocumentCommand{\saveqnumber}{}
 {
  \iow_shipout:cx { @auxout } { \QUESTIONS{\arabic{qsection}}{\arabic{qnumber}} }
 }
\NewDocumentCommand{\QUESTIONS}{mm}
 {
  \seq_gput_right:Nn \g_casper_questions_seq { \RESUMESECTION{#1}{#2} }
  \int_gadd:Nn \g_casper_questions_int { #2 }
 }
\NewDocumentCommand{\RESUMESECTION}{mm}
 {
  \int_compare:nF { #1 = 0 }
   {
    There~are~#2~questions~in~section~\int_to_Alph:n { #1 }.
   }
 }
\NewDocumentCommand{\QSECTIONS} { m }
 {
  \int_gset:Nn \g_casper_sections_int { #1 }
 }
\NewDocumentCommand{\resume}{}
 {
  \begin{flushleft}\scshape
  In~this~paper:\par
  \seq_use:Nn \g_casper_questions_seq { \par }
  \par
  There~are~\int_to_arabic:n { \g_casper_questions_int }~questions~in~total.
  \end{flushleft}
 }

\seq_new:N \g_casper_questions_seq
\int_new:N \g_casper_questions_int
\int_new:N \g_casper_sections_int

\ExplSyntaxOff

%% length setup
\setlength{\parindent}{0pt}
\newlength{\qspace}
\setlength{\qspace}{20pt}

% specific environment
\newcounter{qnumber}
\counterwithin{qnumber}{qsection}

\newenvironment{question}
 {\vspace{\qspace}
  \begin{enumerate}[\bfseries 1\quad][10]%
  \setcounter{enumi}{\value{qnumber}}%
  \item
 }
 {\end{enumerate}
  \filbreak
  \stepcounter{qnumber}
}

\begin{document}

\resume

\qsection{Pure Mathematics}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}


\qsection{XXXXXX}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}


\qsection{XXXXX}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}

\end{document}

在此处输入图片描述

.aux文件包含

\relax 
\QUESTIONS {0}{0}
\QUESTIONS {1}{3}
\QUESTIONS {2}{2}
\QUESTIONS {3}{3}
\gdef \@abspage@last{2}

您可以看到正确的值已被保存并在下次运行 LaTeX 时使用。

如果你想保留\section*并手动编号部分…将后面的代码替换%% infrastructure

%% infrastructure
\newcounter{qsection}
\renewcommand{\theqsection}{\Alph{qsection}}

\NewCommandCopy{\latexsection}{\section}
\renewcommand{\section}{%
  % save the value of qnumber
  \saveqnumber
  \stepcounter{qsection}%
  \latexsection
}
\AtEndDocument{\saveqnumber}

此后代码相同。

问题计数器未重置的版本。

\documentclass[a4paper, 12pt]{article}
\usepackage{amsmath,amssymb}
\usepackage{enumerate}
\usepackage[flushleft]{paralist}[2013/06/09]

%% infrastructure
\newcounter{qsection}
\renewcommand{\theqsection}{\Alph{qsection}}

\NewCommandCopy{\latexsection}{\section}
\renewcommand{\section}{%
  % save the value of qnumber
  \saveqnumber
  \stepcounter{qsection}%
  \latexsection
}
\AtEndDocument{\saveqnumber}

\ExplSyntaxOn
\NewDocumentCommand{\saveqnumber}{}
 {
  \iow_shipout:cx { @auxout } { \QUESTIONS{\arabic{qsection}}{\arabic{qnumber}} }
 }
\NewDocumentCommand{\QUESTIONS}{mm}
 {
  \seq_gput_right:Nx \g_casper_questions_seq
   {
    \RESUMESECTION{#1}{ \int_eval:n { #2 - \g_casper_questions_int } }
   }
  \int_gset:Nn \g_casper_questions_int { #2 }
 }
\NewDocumentCommand{\RESUMESECTION}{mm}
 {
  \int_compare:nF { #1 = 0 }
   {
    There~are~#2~questions~in~section~\int_to_Alph:n { #1 }.
   }
 }
\NewDocumentCommand{\QSECTIONS} { m }
 {
  \int_gset:Nn \g_casper_sections_int { #1 }
 }
\NewDocumentCommand{\resume}{}
 {
  \begin{flushleft}\scshape
  In~this~paper:\par
  \seq_use:Nn \g_casper_questions_seq { \par }
  \par
  There~are~\int_to_arabic:n { \g_casper_questions_int }~questions~in~total.
  \end{flushleft}
 }

\seq_new:N \g_casper_questions_seq
\int_new:N \g_casper_questions_int
\int_new:N \g_casper_sections_int

\ExplSyntaxOff

%% length setup
\setlength{\parindent}{0pt}
\newlength{\qspace}
\setlength{\qspace}{20pt}

% specific environment
\newcounter{qnumber}

\newenvironment{question}
 {\vspace{\qspace}
  \begin{enumerate}[\bfseries 1\quad][10]%
  \setcounter{enumi}{\value{qnumber}}%
  \item
 }
 {\end{enumerate}
  \filbreak
  \stepcounter{qnumber}
}

\begin{document}

\resume

\section*{Section A: Pure Mathematics}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}


\section*{Section B: XXXXXX}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}


\section*{Section C: XXXXX}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}

\begin{question}
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
some texts some texts some texts some texts some texts 
\end{question}

\end{document}

在此处输入图片描述

相关内容