4

I am writing my text in a large numbered list. I can reference another number by \label and ref, which gives me the number (which is perfect). But would it be possible to change that to infra/supra (depending on whether the number comes later on in the document or before) + some static text + the number of the referenced \item?

So what I would like to make LaTeX print is:

\item [text]
\item [text]
\item [text]
\item [text] \label{1}
\item [text]
\item [text]
\item [text] \ref{1} ⇒ supra *static text* 4
\item [text] \ref{2} ⇒ infra *static text* 11
\item [text]
\item [text]
\item [text] \label{2}
\item [text]
4
  • Does it matter of it occurs on a different page?
    – Werner
    Mar 28 '17 at 15:54
  • No, it has to work across the entire document. I reference that number in stead of the pagenemer, as there can be several such numbers on a page
    – Andrew
    Mar 28 '17 at 15:55
  • What does *static text* stand for? Is it the same text string in all cross-references? Separately, if I understand your objective correctly, do you want to (a) automatically generate a \label instruction for each and every \item instruction and (b) prefix the strings "infra" and "supra" to the cross-referencing call-outs depending on whether the item being cross-referenced occurs before or after the current item? Please confirm (or correct).
    – Mico
    Mar 28 '17 at 16:08
  • No, I will add the \labels myself. Every \label will be 'inside' an \item. A \ref should output infra/supra depending on whether the referenced label occurs before or after it, followed by, as you said, a static string (just the same text for every reference) and then the number of the referenced \item(not the page number, the number of the \item)
    – Andrew
    Mar 28 '17 at 17:19
5

With etoolbox you can keep a list of labels that have been defined in the document so far, and display a different string depending on whether a label is in the list or not.

MWE:

\documentclass{article}
\usepackage{etoolbox}

\newcommand{\storelabel}[1]{%
    \label{#1}%
    \listcsgadd{Stored}{#1}%
}
\newcommand{\storeref}[1]{%
    \ifinlistcs{#1}{Stored}{supra}{infra} static text \ref{#1}%
}

\begin{document}
\begin{enumerate}
\item text
\item text \storelabel{first}
\item text
\item text \storeref{first}
\item text
\item text \storeref{second}
\item text
\item text \storelabel{second}
\end{enumerate}
\end{document}

Result:

enter image description here

Note that two new commands are defined instead of \label and \ref. If you want, you can also patch the existing commands to include the list processing (see the etoolbox manual).

3

An approach using refcount:

enter image description here

% MWE taken from http://tex.stackexchange.com/a/360869/5764
\documentclass{article}

\usepackage{refcount}

\newcommand{\storelabel}{\label}
\newcommand{\storeref}[1]{%
  \mbox{}% Not needed if you always have text in an \item
  \ifnum\getrefnumber{#1}<\value{enumi}\relax supra \else infra \fi
  static text \ref{#1}%
}

\begin{document}

\begin{enumerate}
  \item text
  \item text \storelabel{first}
  \item text
  \item text \storeref{first}
  \item text
  \item text \storeref{second}
  \item text
  \item text \storelabel{second}
\end{enumerate}

\end{document}

It probably won't work as expected in nested lists. But one can update it to accommodate for that, if needed.

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.