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}
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....(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.