10

Apologies if this has been asked before, but I simply can't find it, despite voluminous searching. Some question answer bits of my question, but not the whole. The problem is this. I want a counter which is effectively non-numeric, with arbitrary (text) names, which can be referenced. E.g., in a table:

\begin{tabular}{ll}
Project-Quick  \label{quick} & [Details here.] \\
Task-Brown     \label{brown} & [Details here.] \\ 
Todo-Fox       \label{fox}   & [Details here.] \\
Project-Jumped \label{jump}  & [Details here.] \\
\end{tabular}

Where: "\ref{jump} has been completed.", would produce the output: "Project-Jumped has been completed."

Is this possible?

12

The \label macro cannot know what you are tagging unless you mark the relevant text somehow. For example \section tags the section number as something that \label can name.

Here I setup a new reference system for projects so that you can tag things with \project and optionally tag them.

\documentclass{article}
\title{My notes for Bla}
\author{myself}

\newcommand{\projectnamestyle}[1]{\textsc{#1}}
\newcommand{\projectnamerefstyle}[1]{[\textsc{#1}]}
\newcounter{project}

    \newcommand{\project}[1]{%
      \projectnamestyle{#1}%
       \renewcommand{\theproject}{\projectnamerefstyle{#1}}%
       \refstepcounter{project}%
    }

\begin{document}
\begin{tabular}{ll}
  \project{Project-Quick}  \label{quick} & [Details here.] \\
  \project{Task-Brown}     \label{brown} & [Details here.] \\
  \project{Todo-Fox}       \label{fox}   & [Details here.] \\
  \project{Project-Jumped} \label{jump}  & [Details here.] \\
\end{tabular}

And \ref{jump} has been completed.
\end{document}

You can customize (or remove) \projectnamestyle to set the appearances of project names. You can similarly change how references to project names are typeset by changing the definition of \projectnamerefstyle (here it surrounds the reference by square brackets just for illustration).

The key here is calling \refstepcounter{YOURCOUNTER} that makes the next \label to pick up whatever is "stored" in \theYOURCOUNTER.

Update thanks to @egreg

The previous solution is correct but makes use of a counter which in this case is not necessary since the reference does not require automatic numbering.

A more succinct solution is to define

\newcommand{\projectnamestyle}[1]{\textsc{#1}}
\newcommand{\projectnamerefstyle}[1]{[\textsc{#1}]}

\makeatletter
\newcommand{\project}[1]{%
  \projectnamestyle{#1}%
  \def\@currentlabel{\projectnamerefstyle{#1}}%
}
\makeatother

since redefining \@currentlabel is the only effect of \refstepcounter that we were actually using.

  • 1
    You could simply do \def\@currentlabel{\projectnamerefstyle{#1}}, without using a counter and redefining \theproject, because \label picks up whatever is the current expansion of \@currentlabel. – egreg May 20 '14 at 11:22
  • @egreg interesting. I guess this is still useful if the OP wants to switch to some automatic counting in the future. – Bordaigorl May 20 '14 at 11:25
  • Almost there I think. In trying out the more succinct version, I get "! LaTeX Error: \theproject undefined.". In trying out the version with a counter, it works perfectly, but encloses the referred project in square brackets, which I'd like to be rid of. – Dr Raveem Ismail May 20 '14 at 12:15
  • 1
    Remember \makeatletter and \makeatother! – egreg May 20 '14 at 12:25
  • 2
    Sometimes you learn more by answering than by asking =D this community is great! – Bordaigorl May 20 '14 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.