General ⇒ Reference before the Label
Reference before the Label
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.
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
- Stefan Kottwitz
- Site Admin
- Posts: 10360
- Joined: Mon Mar 10, 2008 9:44 pm
Reference before the Label
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}
Reference before the Label
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}
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}
Reference before the Label
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.zanini wrote:but X appears as 3, no matter how much items I have on the list.
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.)