How to create labels and references automatically in LaTeX?

I have defined two custom macros:

\translation{ORIGINAL}{TRANSLATION}
\printtranslations


The first macro prints out the translation and puts the original and the translation into a list. The list can be printed using the \printtranslations macro.

This is working great. However i also want to be able to jump inside the document between translation and original.

I tried using a refcounter but I couldn't figure out how to use it. At the moment I do not create labels automatically and always using the same reference to jump to the translation.

Here is my current code

\documentclass{article}
\usepackage{etoolbox}
\usepackage{hyperref}
\usepackage{tabularx}
\usepackage{forloop}

% Macro creating a translation.
\def\mytranslations{}
\newcommand{\translation}[2]{                                   %
#2                                                          %
\textsuperscript{                                           %
\hspace{-0.2cm}                                         %
}                                                           %
}

% Macro showing all translations.
\newcommand{\showtranslations}[1]{#1}
\newcommand{\printtranslations}{                                %
{                                                           %
\begin{tabularx}{\textwidth}{XX}                        %
\textbf{Original} & \textbf{Translation} \\         %
\hline                                              %
\forlistloop{\showtranslations}{\mytranslations} \\ %
\end{tabularx}                                          %
}                                                           %
}

\begin{document}
\clearpage
\translation{Good}{TeX} and \translation{Better}{LaTeX}
\clearpage
\hypertarget{chap:Translations}
\printtranslations
\end{document}


Can someone pinpoint me into the right direction how to do this?

• Welcome to TeX SE! Could you please complete your code so we have a working example? – cfr Jan 5 '15 at 15:23
• I fear, you will get into some expansion problems – user31729 Jan 5 '15 at 16:37
• @cfr: Thx! I've added a minimal example. – rednammoc Jan 6 '15 at 1:53
• @rednammoc: No, you have just copied part of my solution ;-) – user31729 Jan 6 '15 at 7:09
• @ChristianHupfer: The similarity is just a coincidence ;) – rednammoc Jan 6 '15 at 15:03

This is not the best solution but only a trial --> I splitted the original/translation pair into two lists which has to be traversed separately but allow for later transformation more easily.

Both lists are done in outer loop inside the table.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{longtable}
\usepackage{soul}
\usepackage{tabu}
\usepackage{forloop}

\usepackage{hyperref}

\newcounter{translation}

%\gdef\mytranslations{}
\newcommand{\translation}[2]{%
\refstepcounter{translation}%
{\sethlcolor{Snow2}\hl{#2}}%\bookmark[dest=chap:Translations::\number\value{translation}]{#2}}%
}

\newcommand{\showorig}[2]{%
\stepcounter{seconddummycounter}%
\ifnumequal{\value{seconddummycounter}}{#1}{%
\phantomsection\hypertarget{chap:Original::\number\value{seconddummycounter}}{#2}%
}{%
}%
}%

\newcommand{\showtrans}[2]{%
\stepcounter{seconddummycounter}%
\ifnumequal{\value{seconddummycounter}}{#1}{%
#2%
}{%
}%
}%

\newcounter{dummycounter}%
\newcounter{seconddummycounter}%

\newcommand{\showall}{%
\forloop{dummycounter}{1}{\number\value{dummycounter} < \numexpr\number\value{translation}+1}{%
\setcounter{seconddummycounter}{0}  \forlistloop{\showorig{\number\value{dummycounter}}}{\myoriginals} & \setcounter{seconddummycounter}{0} \forlistloop{\showtrans{\number\value{dummycounter}}}{\mytranslations} \tabularnewline
\hline
}%
}%

% Macro showing all translations.

\newcommand{\printtranslations}{%
{%                                                           %
\tabulinesep=1.8mm                                      %
\begin{longtabu}{XX}                                    %
\textbf{Original} & \textbf{Translation} \tabularnewline         %
\hline                                              %
\showall
\end{longtabu}                                          %
}                                                           %
}

\begin{document}
\clearpage
\translation{Good}{TeX}
\translation{Very Good}{LaTeX}%
\clearpage

\clearpage
\printtranslations

\end{document}


The additional newline after the second column is a feature of longtabu I don't understand at the moment. Using a standard tabular it doesn't appear.

• Thx for your answer and sorry for not providing any minimal example in the initial post. – rednammoc Jan 6 '15 at 15:07
• Why do you initialize a second counter ("seconddummycounter"). – rednammoc Jan 6 '15 at 15:27
• @rednammoc: It's possible without doing it, most probably, but it took some time to understand what you want to achieve – user31729 Jan 6 '15 at 15:29
• @chistian-hupfer: to suppress the additional new-line the \noindent-command can be used. thx again for your answer. – rednammoc Jan 6 '15 at 16:58

This approach is hardly inspired by the answer of Christian Hupfer whereby only one list is used which is a little bit closer to the code of the original question.

Following things were discovered answering this question:

• \ref requires a \label, whereby \hyperlink requires a \hypertarget.
• use \phantomsection in conjunction with \hyperlink and \hypertarget to get better results (as described in this answer).
• do not let the \hyperlink-argument span over multiple lines of code since linebreaks might be recognized as part of the link.

Example:

\hyperlink{
Chap:Translations
}


is not the same as

\hyperlink{Chap:Translations}

• make use of the \noindent-command to suppress indentation.

Code:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{hyperref}
\usepackage{tabularx}
\usepackage{forloop}

\def\mytranslations{} %
\newcounter{mytranslationcounter} %
\newcommand{\translation}[2]{ %
\refstepcounter{mytranslationcounter} %
#2 %
\textsuperscript{\hspace{-0.2cm}( %
{Translation \number\value{mytranslationcounter}} %
)} %
}

% Macro showing all translations
\newcounter{seconddummycounter} %
\newcommand{\showtrans}[2]{ %
\stepcounter{seconddummycounter} %
\ifnumequal{\value{seconddummycounter}}{#1}{ %
\noindent\phantomsection\hypertarget{chap:Translations::\number\value{seconddummycounter}}{#2} %
}{} %
} %
\newcounter{mytranslationindex} %
\newcommand{\showtranslations}{ %
\forloop{mytranslationindex}{1} %
{\number\value{mytranslationindex} < \numexpr\number\value{mytranslationcounter}+1}{ %
\setcounter{seconddummycounter}{0} %
\forlistloop{\showtrans{\number\value{mytranslationindex}}}{\mytranslations} \tabularnewline %
} %
} %
\newcommand{\printtranslations}{                                %
{                                                           %
\begin{tabularx}{\textwidth}{XX}                        %
\textbf{Original} & \textbf{Translation} \\         %
\hline                                              %
\showtranslations \\                                %
\end{tabularx}                                          %
}                                                           %
}

\begin{document}
\newpage
\section{Text}