表格计数器问题

表格计数器问题

编辑:由于下面的例子很长,我在下面做了一个最小的例子,以便更容易地看到问题:

在下面的例子中,计数器的预期值应该是 0、1 和 2,但事实并非如此。第一个表使用 tabular 制作,而出现错误的第二个表使用 tabulary 制作。

最小示例

\documentclass[]{article}

\usepackage{tabulary}
\newcounter{mycounter}

\begin{document}

\arabic{mycounter} % should be 0

\begin{tabular}{cc}
a\stepcounter{mycounter} & b 
\end{tabular}

\arabic{mycounter} % should be 1

\begin{tabulary}{\textwidth}{JJ}
a\stepcounter{mycounter} & b 
\end{tabulary}

\arabic{mycounter} % should be 2, as we have only increased the counter once since the value was 1

\end{document}

当试图想出一个答案时此主题,我写了以下一段代码。它的目的是排版一些称为 sidenotes 的东西,它的编号应该或多或少地与脚注的编号一样。但是,出于某种原因,命令 \sidenote 似乎对每个注释调用两次,导致每个注释的编号增加了每个段落中的注释数(见下图)。有没有简单的方法可以解决这个问题(即我是否遗漏了一些简单的东西)?

\documentclass[a4paper]{article}

\usepackage[margin=3cm]{geometry}

\usepackage{tabulary}
\usepackage[newcommands]{ragged2e}
\usepackage{array}

\tymin=0.1\textwidth
\tymax=10\textwidth
\renewcommand{\tabcolsep}{0cm}
\newcommand{\colspace}{1em}
\AtBeginDocument{\edef\normalparindent{\the\parindent}}

\newcounter{localsidenotecounter}

\newwrite\tempfile

\newcommand{\sidenote}[1]
{%
\stepcounter{localsidenotecounter}%
${}^\textrm{\footnotesize\arabic{localsidenotecounter}}$%
\immediate\write\tempfile%
{(\arabic{localsidenotecounter}) #1

}%
}

\newcommand{\sidenotepc}[1]
{
\noindent \begin{tabulary}{\textwidth}{>{\setlength\parindent{\normalparindent} \immediate\openout\tempfile=sidenotes.tex }J<{\immediate\closeout\tempfile}@{\hspace{\colspace}}>{
\footnotesize \input{sidenotes.tex}
}J}
#2 & \end{tabulary}
}


\begin{document}

\sidenotepc{
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue\sidenote{Test} pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure\sidenote{Test} him some great pleasure\sidenote{Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure}.
}


\sidenotepc{
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue\sidenote{Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure} pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure\sidenote{Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure}
}

\end{document}

错误票据编号示​​例

答案1

tabularx我本来可以发誓我偷了试运行后重置 LaTeX 计数器的代码,但显然并非如此......

在此处输入图片描述

(请注意,您可能应该使用它\jobname.foo作为临时文件名,运行时它会覆盖该名称的任何文件,这有点可怕。

\documentclass[a4paper]{article}

\usepackage[margin=3cm]{geometry}

\usepackage{tabulary}
\usepackage[newcommands]{ragged2e}
\usepackage{array}

\tymin=0.1\textwidth
\tymax=10\textwidth
\renewcommand{\tabcolsep}{0cm}
\newcommand{\colspace}{1em}
\AtBeginDocument{\edef\normalparindent{\the\parindent}}

\newcounter{localsidenotecounter}

\newwrite\tempfile
\makeatletter

\def\endtabulary{%
  \gdef\@halignto{}%
  \def\@elt##1{\global\value{##1}\the\value{##1}\relax}%
  \edef\TY@ckpt{\cl@@ckpt}%
  \expandafter\TY@tab\the\toks@
  \crcr\omit
  {\xdef\TY@save@row{}%
     \loop
    \advance\TY@count\m@ne
    \ifnum\TY@count>\z@
    \xdef\TY@save@row{\TY@save@row&\omit}%
    \repeat}\TY@save@row
  \endarray\global\setbox1=\lastbox\setbox0=\vbox{\unvbox1
    \unskip\global\setbox1=\lastbox}\egroup
  \dimen@\TY@linewidth
  \divide\dimen@\TY@count
  \ifdim\dimen@<\tymin
    \TY@warn{tymin too large (\the\tymin), resetting to \the\dimen@}%
    \tymin\dimen@
  \fi
  \setbox\tw@=\hbox{\unhbox\@ne
    \loop
\@tempdima=\lastskip
\ifdim\@tempdima>\z@
   \global\advance\TY@linewidth-\@tempdima
\fi
    \unskip
    \setbox\tw@=\lastbox
    \ifhbox\tw@
      \ifdim\wd\tw@>\tymax
        \wd\tw@\tymax
      \fi
  \TY@width\dimen@
  \advance\dimen@\wd\tw@
   \TY@width\xdef{\the\dimen@}%
      \ifdim\dimen@<\tymin
         \global\advance\TY@linewidth-\dimen@
         \expandafter\xdef\csname TY@F\the\TY@count\endcsname
                                                        {\the\dimen@}%
       \else
      \expandafter\ifx\csname TY@F\the\TY@count\endcsname\z@
         \global\advance\TY@linewidth-\dimen@
         \expandafter\xdef\csname TY@F\the\TY@count\endcsname
                                                        {\the\dimen@}%
        \else
         \global\advance\TY@tablewidth\dimen@
         \global\expandafter\let\csname TY@F\the\TY@count\endcsname
                                                               \maxdimen
       \fi\fi
       \advance\TY@count\m@ne
    \repeat}%
    \TY@checkmin
    \TY@checkmin
    \TY@checkmin
    \TY@checkmin
    \TY@count\z@
    \let\TY@box\TY@box@v
  \TY@ckpt
  {\expandafter\TY@final\the\toks@\endTY@final}%
  \count@\z@
  \@tempswatrue
  \@whilesw\if@tempswa\fi{%
  \advance\count@\@ne
  \expandafter\ifx\csname TY@SF\the\count@\endcsname\relax
    \@tempswafalse
  \else
    \global\expandafter\let\csname TY@F\the\count@\expandafter\endcsname
                   \csname TY@SF\the\count@\endcsname
    \global\expandafter\let\csname TY@\the\count@\expandafter\endcsname
                   \csname TY@S\the\count@\endcsname
  \fi}%
  \TY@linewidth\@ovxx
  \TY@tablewidth\@ovyy
    \ifnum0=`{\fi}}

\newcommand{\sidenote}[1]
{%
\stepcounter{localsidenotecounter}%
${}^\textrm{\footnotesize\arabic{localsidenotecounter}}$%
\immediate\write\tempfile%
{(\arabic{localsidenotecounter}) #1

}%
}

\newcommand{\sidenotepc}[1]
{
\noindent \begin{tabulary}{\textwidth}{>{\setlength\parindent{\normalparindent} 
\ifx\@classz\TY@classz
\immediate\openout\tempfile=sidenotes.tex \fi}J<{\immediate\closeout\tempfile}@{\hspace{\colspace}}>{
\footnotesize \input{sidenotes.tex}
}J}
#1 & \end{tabulary}
}

\makeatother

\begin{document}

\sidenotepc{
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue\sidenote{Test} pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure\sidenote{Test} him some great pleasure\sidenote{Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure}.
}


\sidenotepc{
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue\sidenote{Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure} pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure\sidenote{Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure}
}

\end{document}

相关内容