# Counter problem with tabulary

EDIT: As the example below was quite long, I made a minimal example below to make it easier to see the problem:

In the example below, the expected values of the counters should be 0, 1 and 2, but its not. The first table is made with tabular and the second, where the error occurs, with 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}


When trying to come up eith an answer to this thread, I wrote the following piece of code. The purpose of it is to typeset something called sidenotes which whose numbering should work more or less as the numbering of footnotes. However, for some reason, the command \sidenote seems to be called twice for each note, resulting in the number for each note being increased by the number of notes in each paragraph (see image below). Is there any simple way to fix this (i.e. am I missing something simple)?

\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}


I could have sworn that I stole the code from tabularx that resets LaTeX counters after a trial run but apparently not so....

(Note you should probably use \jobname.foo as the temporary file name, it is a bit scary that it overwrites any file of that name when run.

\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
\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@
\fi
\unskip
\setbox\tw@=\lastbox
\ifhbox\tw@
\ifdim\wd\tw@>\tymax
\wd\tw@\tymax
\fi
\TY@width\dimen@
\TY@width\xdef{\the\dimen@}%
\ifdim\dimen@<\tymin
\expandafter\xdef\csname TY@F\the\TY@count\endcsname
{\the\dimen@}%
\else
\expandafter\ifx\csname TY@F\the\TY@count\endcsname\z@
\expandafter\xdef\csname TY@F\the\TY@count\endcsname
{\the\dimen@}%
\else
\global\expandafter\let\csname TY@F\the\TY@count\endcsname
\maxdimen
\fi\fi
\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{%
\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}

• Thanks! (I'll try to do a more minimal file next time... :) ) – malin Jun 17 '13 at 16:22
• @malin the test file was fine (apart from the fact it generated an error with a spurious #2 in a definition) It would be possible to re-package the answer with a "patch" to \endtabulary` but it was easier to directly edit the full version. – David Carlisle Jun 17 '13 at 16:25