2

Is there a way to assign a reference to something (using \label), but to not actually have it show up in the document? I would still like to cross-reference it in a later part of the document?

For example, I have an counter called \productitem, and I'd like to do something like this:

\productitem\label{initialitem}\\ %Lorem ipsum

Then later in the document:

\ref{initialitem} is sold...

Now this code would print a reference number for initialitem, and then later on reference that number. However, I don't want the initial reference number to be printed, nor do I want any space given to it or any blank lines.

I realise you might wonder why I want to do this. I am producing a product listing; the initial product not available (or is not relevant to a particular listing) and so can't be listed. However, a variant of it does need to be listed. I would like to use the initial product's reference number as a basis for the variant's product number.

2
  • 5
    Could you please add a minimal compilable document that shows what you're doing (even if it doesn't work the way you want). Especially because your question title doesn't make too much sense: \label itself never outputs anything, only \ref does.
    – Alan Munn
    Nov 14, 2017 at 1:47
  • Reading this question for the 5th time and I don't understand it...
    – user31729
    Nov 14, 2017 at 21:18

2 Answers 2

2

You can define a counter and let \productitem just \refstepcounter the counter:

\documentclass{article}

\newcounter{productitem}
\makeatletter
\newcommand{\productitem}{\@bsphack\refstepcounter{productitem}\@esphack}
\makeatother

\begin{document}

\productitem\label{initialitem}

See item \ref{initialitem}.

\end{document}

If the only use of \productitem is to set a new product and will always be followed by a \label, it's better (and more convenient) to set a macro to do both:

\documentclass{article}

\newcounter{productitem}
\newcommand{\productitem}{\refstepcounter{productitem}\label}

\begin{document}

\productitem{initialitem}

See item \ref{initialitem}.

\end{document}
4
  • Why \unskip and not \ignorespaces? Just curious. Nov 14, 2017 at 4:38
  • @JohnKormylo: I wanted to treat it similar to how one would \label. I've updated it to do exactly that, taking it from latex.ltx.
    – Werner
    Nov 14, 2017 at 6:24
  • if used in this combination \productitem\label{initialitem}, the \@bsphack/\@esphack serves nothing special. It is done by \label itself to avoid double spaces in foo \label{A} bar situation. So \refstepcounter{bbbb}\label{A} is no different from \label{A} alone in that regard and needs no extra space hack.
    – user4686
    Nov 14, 2017 at 8:31
  • 1
    @jfbu: Indeed. I don't know the use-case, but have added some more content.
    – Werner
    Nov 14, 2017 at 16:08
0

An obvious modification of the \refstepcounter command will set the current reference to the value of a given counter, without incrementing it; then \label can be issued as usual to capture the current reference value. Here is an example of this technique:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\makeatletter

% Define core functionality:
\newcommand*\refbutnotstepcounter[1]{%
    \protected@edef\@currentlabel
        {\csname p@#1\endcsname\csname the#1\endcsname}%
}

\makeatother

% Define handy shortcut:
\newcommand*{\photocnt}[2]{%
    \refbutnotstepcounter{#1}\label{#2}%
}

% Test counter:
\newcounter{FOO}



\begin{document}

\setcounter{FOO}{123}\photocnt{FOO}{value-1}
\addtocounter{FOO}{300}\photocnt{FOO}{value-2}

The first value was $\ref{value-1}$, the second (and current) value is
$\ref{value-2}$.

\stepcounter{FOO}\photocnt{FOO}{value-3}
\setcounter{FOO}{-1}
In mid-paragraph \photocnt{FOO}{value-4} too.
\addtocounter{FOO}{100}\photocnt{FOO}{value-5}

We have now three more ``photographs'': $\ref{value-3}$, $\ref{value-4}$, and
$\ref{value-5}$.

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .