2

I would like to create a table of references. The problem is, of course, that \label gives me the section number, but I have several items in the same section that I want to reference. Is there some way to define my own labels? Thanks.

\documentclass{article}
\begin{document}

\section{First}
See project\label{p.1}\ref{p.1}
See project\label{p.2}\ref{p.2}

\section{Second}
See project\label{p.3}\ref{p.3}
See project\label{p.4}\ref{p.4}

\section{Tables}
\begin{tabular}{ll}
Project & Link\\
\ref{p.1} & xxx\\
\ref{p.2} & yyy\\
\ref{p.3} & zzz\\
\ref{p.4} & uuu\\
\end{tabular}
\end{document}
Moti
  • 91
  • 1
    you can put labels anywhere. What exactly do you want to appear when you reference it? Are you looking for \pageref? And what do you want to achieve with referencing the label just right where it is? (in your sections First and Second) – riddleculous Dec 16 '19 at 10:57
  • 1
    you say of course \label produces a section number but it only does that as you have placed it after \section you can \label and \ref any numbered latex construct such as tables, list items etc, however it is totally unclear what output you expect from the code shown, I would guess that \label{p.1}\ref{p.1} should be simply \ref{p.1} to reference the items and your table should be a \begin{itemize} with numbered items with eg \item\label{p.1} so that the ref refers to this item. (It is normally bad style to use numbers in label and ref keys) – David Carlisle Dec 16 '19 at 12:48
  • Clarification: The elements are not contained in an environment like itemize. Scattered through the text I want "See project n" where n is incremented as it appears. Then I want an appendix listing all the projects and links to them. Counters would be great for incrementing n but I can't see any way to "construct" a label from a counter. (The numbers in the label are just to give a minimal example; I am using better keys.) – Moti Dec 16 '19 at 16:16

1 Answers1

2

When you use the macro \refstepcounter instead of \stepcounter for incrementing a counter, then \the⟨counter⟩ of the incremented counter will be available for \label.

You can use the hyperref package for having hyperlinks in your document.

Perhaps this is what you are after? :

\documentclass{article}
\usepackage{hyperref}

\newcounter{project}

\begin{document}

\section{First}
See \refstepcounter{project}project~\theproject\label{p.1}
See \refstepcounter{project}project~\theproject\label{p.2}

\section{Second}
See \refstepcounter{project}project~\theproject\label{p.3}
See \refstepcounter{project}project~\theproject\label{p.4}

\section{Tables}
\begin{tabular}{ll}
Project & Link\\
\ref*{p.1} & \hyperref[p.1]{xxx}\\
\ref*{p.2} & \hyperref[p.2]{yyy}\\
\ref*{p.3} & \hyperref[p.3]{zzz}\\
\ref*{p.4} & \hyperref[p.4]{uuu}\\
\end{tabular}
\end{document}

enter image description here

In case you are interested in how cross-referencing is implemented in LaTeX: I tried to explain the basic concepts related to LaTeX's cross-referencing mechanism in my answer to the question "How to prevent reference to enumeration inside new environment?".

Ulrich Diez
  • 28,770
  • Thanks, Ulrich! I can't remember if I ever used \refstepcounter in 30-odd years of working with LaTeX, but it is never too late to learn something new. – Moti Dec 17 '19 at 07:38
  • @Moti Learn something else: Mark Ulrich's answer as good and give it an upvote, too! – Keks Dose Dec 17 '19 at 20:06