General ⇒ Changing how a cross-reference is printed
Changing how a cross-reference is printed
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.
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.
NEW: TikZ book now 40% off at Amazon.com for a short time.

Changing how a cross-reference is printed
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}
Changing how a cross-reference is printed
I assume that the following in the relevant part:
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.
Code: Select all
\makeatletter
\renewcommand{\p@enumii}{\theenumi.-}
\makeatother
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.
Changing how a cross-reference is printed
Sorry. Sometimes I think the code is self-explanatory. Surely this is not the case. So let's detail it a bit.Do you think you can explain to me what this does so I can learn the idea behind it.
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})}
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
Code: Select all
\makeatletter
\renewcommand{\p@enumii}{\theenumi.-}
\makeatother
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.
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.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?
I hope things are now well explained. If not, please ask again.
Re: Quick cross reference question
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?
I guess this is causing the loss of the paraetheses ... something different in the shortlst package. Any suggestions for a quick fix?
Changing how a cross-reference is printed
What about this?
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.
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}
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.
Re: Quick cross reference question
Does that mean I would have to include that in every shortenumerate I have separately?
Changing how a cross-reference is printed
I was editing my last post while you asked this question. The answer is there.dmt wrote:Does that mean I would have to include that in every shortenumerate I have separately?
Changing how a cross-reference is printed
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:
But the output of my lists appears as:
etc.
What am I doing wrong?
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
Code: Select all
1. Item one
(1) Sub item one
1. Sub sub item one
(2) Sub item two
2. Item two
What am I doing wrong?