4

The below code produces

enter image description here

I'm wondering if it's possible to have the subsections show up as i, ii, etc, on the subsection title (because it's clear which section they're in when they show up), but as 1.i, 1.ii, etc. when I refer to them. (So that the reader can distinguish between 1.i and 2.i, for instance.)

Possibilities:

  1. Just enter the section number manually when I refer to it. This is worst, since it isn't robust to renumbering the sections.
  2. Find a way to store the section number of Fruit Section, and use that when I make the reference. This is OK, but not robust to changing which section Apples is a part of.
  3. Inside Apples, get the current section somehow, store it, and then refer to it. This would be robust, but would have to be done every time I want to make such a reference.
  4. Better once-and-for-all solution to this problem.

.

\documentclass{article}
\usepackage{hyperref}
\renewcommand\thesubsection{\roman{subsection}}
\begin{document}
\section{Fruit Section}
\subsection{Apples} \label{Apples-subsec} 
\subsection{Bananas} \label{Bananas-subsec}
\section{Vegetable Section}
\subsection{Kale}
\section{Conclusion}
 As we recall from sections \ref{Apples-subsec} and \ref{Bananas-subsec}, apples and bananas are healthy to eat.
\end{document}
  • 1
    I general I think users are going to be confused when they are browsing through the document that there are more than one section that are named i. It is probably doable using some hackery,but I would not recommend it. – daleif Jun 21 '18 at 14:45
  • @daleif I for one bin such documents...if the author cannot be bothered to use proper structure in the document, why should I be bothered reading it? – naphaneal Jun 21 '18 at 16:26
  • @naphaneal there apparently is a kind of "cult" around this way of numbering, I have a colleague who adhere to it. But as mentioned, I pity the reader. – daleif Jun 21 '18 at 16:28
4

You can use \p@<counter> to add something before the actual counter value in references. To quote source2e (§22.1)

\p@foo Macro that expands to a printed ‘reference prefix’ of counter foo. Any \ref to a value created by counter foo will produce the expansion of \p@foo\thefoo when the \label command is executed.

So

\makeatletter
\renewcommand*{\p@subsection}{\thesection.}
\makeatother

would add the section and a dot whenever you reference a subsection.

\documentclass{article}
\usepackage{hyperref}
\renewcommand\thesubsection{\roman{subsection}}
\makeatletter
\renewcommand*{\p@subsection}{\thesection.}
\makeatother
\begin{document}
\section{Fruit Section}
\subsection{Apples} \label{Apples-subsec} 
\subsection{Bananas} \label{Bananas-subsec}
\section{Vegetable Section}
\subsection{Kale}
\section{Conclusion}
 As we recall from sections \ref{Apples-subsec} and \ref{Bananas-subsec}, apples and bananas are healthy to eat.
\end{document}

enter image description here

Having seen the result I would probably not do this, especially if the subsections can be longer, and would include the section number for subsections also in the headings.

  • Is there a way to show the section number for only one reference in the document, but not in general? I tried putting \makeatletter \renewcommand... inside a group along with the reference I'm making, but it didn't work. (I'm guessing \makeatletter \renewcommand... wants to be in the preamble.) – Eric Auld Jun 21 '18 at 16:24
  • @EricAuld What exactly are you thinking of? As far as I am aware the \p@foo setting active at the time of defining the \label governs all of its later uses in \ref no matter what their environment setting w.r.t \p@foo is. So if you want something more complicated a different solution is probably needed. Please ask a new question about that. – moewe Jun 21 '18 at 16:29

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.