General ⇒ Changing how a cross-reference is printed
Changing how a cross-reference is printed
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.
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
Changing how a cross-reference is printed
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
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
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
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.
Re: Quick cross reference question
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
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?