GeneralChanging how a cross-reference is printed

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Changing how a cross-reference is printed

Post by dmt »

I'm trying to cross references an item in a nested enumeration that uses the defaults numbering scheme so it looks like this:

8. (a) blah blah

I want to reference a nested item later. Using label and ref though I get something like this:

See problem 8a

I would like to get something like

See problem 8(a)

instead. How do I do this in a simple manner? Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Changing how a cross-reference is printed

Post by Juanjo »

I complete the example I gave in this other thread to meet your new requirements:

Code: Select all

\documentclass{article}

\usepackage{amsmath}
\usepackage{paralist}

\makeatletter
\renewcommand{\p@enumii}{\theenumi.-}
\makeatother

\newenvironment{myinparaenum}%
   {\begin{inparaenum}[\hspace{2em}a]%
     \renewcommand{\theenumii}{(\alph{enumii})}%
     \hspace{-2em}\ignorespaces}%
   {\end{inparaenum}}

\begin{document}

\begin{enumerate}
\item $\begin{aligned}[t]
             a&=b+c+d+e+f+g+h+i \\
               &=j+k+l+m+n+p \\
               &=q+r+s+t
           \end{aligned}$  \label{prob1}
 \item \begin{inparaenum}[a]
           \renewcommand{\theenumii}{(\alph{enumii})}
           \item $\sin \pi/4$ \item $\cos  \pi/7$ \item $\tan 3\pi/5$  \label{prob2}
         \end{inparaenum}
 \item \begin{myinparaenum}
              \item $\sin \pi/4$ \item $\cos  \pi/7$ \item $\tan 3\pi/5$ \label{prob3}
         \end{myinparaenum}
\end{enumerate}

See problem \ref{prob1}. See problem \ref{prob2}. See problem \ref{prob3}.

\end{document}
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Changing how a cross-reference is printed

Post by dmt »

I assume that the following in the relevant part:

Code: Select all

\makeatletter
\renewcommand{\p@enumii}{\theenumi.-}
\makeatother
Do you think you can explain to me what this does so I can learn the idea behind it.

I understand the \makeatletter and \makeatother part, but now what you are putting in the renewcommand command.

---
I tested the code now and it doesn't quite do what I want. I get:

8.-a

when I was hoping more for parentheses around the a. What do I need to change?
--------
okay, through some experimentation I figured out the second part of the command, what \theenumi stands for and what the . and - are doing.

I don't understand the syntax of the first part of the command with \p@ and why the enumii is automatically added at the end here though the enumi has to be specified in the command.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Changing how a cross-reference is printed

Post by Juanjo »

Do you think you can explain to me what this does so I can learn the idea behind it.
Sorry. Sometimes I think the code is self-explanatory. Surely this is not the case. So let's detail it a bit.

The enumeration environment uses a counter to number entries. Since two o more enumerations can be nested, the precise name of the counter depends on the level: by default, they are enumi, enumii, enumiii and enumiv. The paralist package does not change this convention. So, in my code, the enumeration environment (first level) uses the enumi counter, whereas inparaenum and myinparaenum (second level) use enumii. The commands \theenumi and \theenumii control how these counters are printed: by default, \theenumi yields 1, 2, 3, 4... (depending on the value of enumi), and \theenumii gives a, b, c, d, e...

If you look at the inparaenum environment and to the definition of myinparaenum, you'll see the command

Code: Select all

\renewcommand{\theenumii}{(\alph{enumii})}
So, \theenumii prints (a), (b), (c), (d)... The sense of this change will be clear later. By the moment, let's note that the optional argument of the inparaenum should be a bit different: just [a], instead of [(a)], since the parentheses are already incorporated in the format of the counter.

When a \label command captures the value of a counter named ctr, LaTeX saves the value in the format printed by the corresponding \thectr command and automatically prepends the result of an internal command called \p@ctr. When a \ref command writes the saved value of the counter, LaTeX prints \p@ctr\thectr. For many counters, \p@ctr does nothing, but you can always redefine it if it is convenient. Just for fun, put this in the preamble of document with several equations:

Code: Select all

\makeatletter
\renewcommand{\p@equation}{Eq.~}
\makeatother
Then add references to the corresponding equations and see the results. In my code, you find this:

Code: Select all

\makeatletter
\renewcommand{\p@enumii}{\theenumi.-}
\makeatother
Hence, any reference to a second level entry in a numbered list will have a format like 1.-(a), 2.-(d), 3.-(h)..., i.e. the value of enumi (printed by \theenumi), then ".-" and then the value of enumii (printed by \theenumii). If you don't like to have ".-" in such references, just remove it from the definition of \p@enumii.

I think that now it is clear that, in my code, the command \label{prob2} associates with prob2 the value 2.-(c), since at that precise moment the value of enumii is 3 (so \theenumii prints (c)) and the value of enumi is 2 (so \p@enumii prints 2.-). Likewise, \label{prob3} associates with prob3 the value 3.-(c). Consequently, the commands \ref{prob2} and \ref{prob3} write 2.-(c) and 3.-(c). This is what you should get in the last line of the text.
dmt wrote: I tested the code now and it doesn't quite do what I want. I get:

8.-a

when I was hoping more for parentheses around the a. What do I need to change?
Have you tested the code by its own? If you get, say, 8.-a instead of 8.-(a), there are something in your document that is changing the behaviour I expect from \theenumii.

I hope things are now well explained. If not, please ask again.
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Re: Quick cross reference question

Post by dmt »

Thanks, I think I understand most of it now. But I am getting 8.-a instead of 8.-(a). However, my code isn't exactly like yours since I am using shortenumerate now instead of inparaenum (or your version of myinparaenum) since it seemed a more suitable (and already defined) environment.

I guess this is causing the loss of the paraetheses ... something different in the shortlst package. Any suggestions for a quick fix?
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Changing how a cross-reference is printed

Post by Juanjo »

What about this?

Code: Select all

\documentclass{article}

\usepackage{amsmath}
\usepackage{shortlst}

\makeatletter
\renewcommand{\p@enumii}{\theenumi.-}
\makeatother

\begin{document}

\begin{enumerate}
\item First entry.
\item%
\begin{runenumerate}
    \renewcommand{\theenumii}{(\alph{enumii})}
    \renewcommand{\labelenumii}{\theenumii}
    \item $\sin \pi/4$ \item $\cos  \pi/7$ \item $\tan 3\pi/5$  \label{prob2}
\end{runenumerate}
\end{enumerate}

See problem \ref{prob2}.
\end{document}
The \labelenumii commands gives the exact format of the label for each entry in the list. In the inparaenum environment, it is determined from the optional argument. Here we have to deal with it.

Edited: The two \renewcommand inside runenumerate can be put in the preamble. In this case, they will affect all the second level numbered lists. Perhaps that is what exactly you want, so you have not to repeat those commands in every list. You can also define your own environment.
Last edited by Juanjo on Mon Mar 17, 2008 1:18 am, edited 1 time in total.
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Re: Quick cross reference question

Post by dmt »

Does that mean I would have to include that in every shortenumerate I have separately?
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Changing how a cross-reference is printed

Post by Juanjo »

dmt wrote:Does that mean I would have to include that in every shortenumerate I have separately?
I was editing my last post while you asked this question. The answer is there.
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Re: Quick cross reference question

Post by dmt »

Thanks!
NobleMule
Posts: 4
Joined: Thu Oct 09, 2008 10:58 am

Changing how a cross-reference is printed

Post by NobleMule »

I have a similar question to the OP. I want my first level to remain as standard (i.e. just the number). I want my second level to be the number of the first level '.' incrementing number for the second level and so on. Basically, I want the three levels to be numbered like sections, subsections and subsubsections. I've tried to apply the principals above and tailor them to my own needs but it's not coming out how i'd expect.

This is probably completely wrong but I'm a LaTeX n00b so please forgive me.
In my preamble, I have:

Code: Select all

\makeatletter
\renewcommand{\p@enumii}{\theenumi.}
\renewcommand{\theenumii}{\arabic{enumii}}
\renewcommand{\p@enumiii}{\theenumii.}
\renewcommand{\theenumiii}{\arabic{enumiii}}
\makeatother
But the output of my lists appears as:

Code: Select all

1. Item one
    (1) Sub item one
        1. Sub sub item one
    (2) Sub item two
2. Item two
etc.

What am I doing wrong?
Post Reply