[Tex/LaTex] Counter problem with tabulary

counterstablestabulary

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.

Minimal example

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

Example of erranous note numbers

Best Answer

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

enter image description here

(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
    \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}
Related Question