GeneralReference before the Label

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
zanini
Posts: 3
Joined: Mon Dec 12, 2011 4:12 pm

Reference before the Label

Post by zanini »

Hello!
I started using LaTeX not long ago and I couldn't find out how to solve the following problem, even though I have the impression the solution must be pretty simple.

I have a list which has a counter. Once in a while I add an item and the values change.
In my document, before the list I have a line which says "x items in the list". "x" is the number of items.

So, the thing is I would like to automatically update x each time I add an item to the list, but I can't figure out how to put a reference before the label or the counter. If I do it, I get an error because the label/counter is unknown.

Any ideas? Thanks in advance.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Reference before the Label

Post by Stefan Kottwitz »

Hi Zanini,

welcome to the board!

References can be put before a label. Here's an example which shows that it works. Compile two times, because in the first run the forward reference is unknown, in the second run it is.

Code: Select all

\documentclass{article}
\begin{document}
The following list has \ref{itemcount} items.
\begin{enumerate}
  \item One
  \item Two
  \item more
  \item and more\label{itemcount}
\end{enumerate}
\end{document}
Stefan
LaTeX.org admin
zanini
Posts: 3
Joined: Mon Dec 12, 2011 4:12 pm

Reference before the Label

Post by zanini »

Hi Stefan_K,

Thanks for the reply.
Actually, enumerate won't do the thing because my items are inside tables and I can't get the right formatting with it. I need just the numbers, no spaces or dots.
So here's the code:

Code: Select all

X papers published

\newcounter{papers}
\newcommand{\pa}{0.05}
\newcommand{\pb}{0.90}

\begin{tabular}{@{}>{\raggedleft}p{\pa\textwidth} p{\pb\textwidth}@{}}
 \addtocounter{papers}{1}\arabic{papers} & Authors1\tabularnewline
 & \textbf{Title1}\tabularnewline
 & Journal1 \href{run:http://apl.aip.org/resource/1/applab/v99/i23/p232504_s1}{$[$doi:10.1063/1.3664092$]$}\tabularnewline
 &\tabularnewline
\end{tabular}
\\
\begin{tabular}{@{}>{\raggedleft}p{\pa\textwidth} p{\pb\textwidth}@{}}
 \addtocounter{papers}{1}\arabic{papers} & Authors2\tabularnewline
 & \textbf{Title2}\tabularnewline
 & Journal2 \href{run:http://apl.aip.org/resource/1/applab/v96/i10/p102511_s1}{$[$doi:10.1063/1.3341190$]$}\tabularnewline
\end{tabular}
This first "X" appearing in the code should be the number of papers.

Based on the code you've sent, I've tried

Code: Select all

\ref{npapers} papers published

\newcounter{papers}
\newcommand{\pa}{0.05}
\newcommand{\pb}{0.90}

\begin{tabular}{@{}>{\raggedleft}p{\pa\textwidth} p{\pb\textwidth}@{}}
 \addtocounter{papers}{1}\arabic{papers} & Authors1\tabularnewline
 & \textbf{Title1}\tabularnewline
 & Journal1 \href{run:http://apl.aip.org/resource/1/applab/v99/i23/p232504_s1}{$[$doi:10.1063/1.3664092$]$}\tabularnewline
 &\tabularnewline
\end{tabular}
\\
\begin{tabular}{@{}>{\raggedleft}p{\pa\textwidth} p{\pb\textwidth}@{}}
 \addtocounter{papers}{1}\arabic{papers}\label{npapers} & Authors2\tabularnewline
 & \textbf{Title2}\tabularnewline
 & Journal2 \href{run:http://apl.aip.org/resource/1/applab/v96/i10/p102511_s1}{$[$doi:10.1063/1.3341190$]$}\tabularnewline
\end{tabular}
but X appears as 3, no matter how much items I have on the list.
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Reference before the Label

Post by sommerfee »

zanini wrote:but X appears as 3, no matter how much items I have on the list.
Use \refstepcounter{papers} instead of \addtocounter{papers}{1}. This way the counter will be incremented, too, but also a "dog tag" will be generated which can be referenced with \label and \ref.

Furthermore I would replace \arabic{papers} with \thepapers since \thepapers is the macro which will also be used by \ref. (And if you want to change the appearance later on, you only need to redefine \thepapers and not changing the rest of the document.)
zanini
Posts: 3
Joined: Mon Dec 12, 2011 4:12 pm

Re: Reference before the Label

Post by zanini »

Thank you sommerfee. It works perfectly!
Post Reply