1

I have a lot of subsections with the name "Example X.Y" in my thesis and want to use a macro which generates the names and labels for the subsections.

The goal is to be able to reference examples like in the picture below:

The goal

Using the nameref package I tried the following:

\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{nameref}

\newcommand{\example}{\subsection{Example \arabic{section}.\arabic{subsection} }\label{ex:\arabic{section}-\arabic{subsection}}}

\begin{document}

\chapter{Simulations}
\section{Examples 1}
\example
\example
\nameref{ex:1-2} has the same solution as \nameref{ex:1-1}.

\end{document}

This however gives me the text "Example 1.2 has the same solution as Example 1.2".

I suspect that the reference is working as intended, but when substituting the subsection's name Example \arabic{section}.\arabic{subsection} is called again, resulting in only giving me the name of the current example/subsection.

Is there a way to generate subsections and labels with a macro or do I have to do it all by hand?

5
  • Welcome to TeX.SX! Please consider turning your code snippets into a fully compilable example, an MWE. I just tries to do that and failed to reproduce the undesired behaviour you see, so there must be more to it. An MWE makes it easier for us to help you, because we can see the exact same thing that you see.
    – moewe
    May 25, 2018 at 7:55
  • 1
    Your guess is spot on by the way, you can see in the .aux file that the counter values are not expanded in the \newlabel leading to Example \arabic{section}.\arabic{subsection} being the value of both labels.
    – moewe
    May 25, 2018 at 8:10
  • Thank you @moewe, I have added the full source code now.
    – Felix Auer
    May 25, 2018 at 8:16
  • Very good, thank you. You need to expand the counters in the section, so nameref can pick them up properly. I can't think of a nice solution for that, unfortunately.
    – moewe
    May 25, 2018 at 8:19
  • 2
    What's the advantage of this system over directly writing Example~1.2 instead of \nameref{ex:1-2}, as you need to know the actual number?
    – egreg
    May 25, 2018 at 15:45

3 Answers 3

2

You have to expand the subsection title before it is written into the aux file. You can not control writing it, so instead you can expand it before even calling subsection. Of course this means \arabic{subsection} will be the old subsection number, so you have to increase it:

\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{nameref}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \expNx \exp_args:Nx
\ExplSyntaxOff

\newcommand\arabicPlusOne[1]{\number\numexpr1+\value{#1}\relax}
\newcommand\example{\expNx\subsection{Example \arabic{section}.\arabicPlusOne{subsection} }\label{ex:\arabic{section}-\arabic{subsection}}}

\begin{document}

\chapter{Simulations}
\section{Examples 1}
\example
\example
\nameref{ex:1-2} has the same solution as \nameref{ex:1-1}.

\end{document}
1
  • That seems to do the trick! Thank you very much
    – Felix Auer
    May 25, 2018 at 13:27
3

It's not clear what advantage you have in using such an approach, because you need to know the number that's assigned to each particular example in order to use \ref{ex:<section>-<subsection>} and so using directly

Example~1.2

instead of \nameref{ex:1-2}` seems much easier.

You can instead use a symbolic label, which doesn't depend on the actual number that's assigned.

\documentclass[10pt,a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{nameref}

\newcommand{\example}[1]{%
  \subsection{Example \ref{ex:#1}}%
  \label{ex:#1}%
}
\renewcommand\thesubsection{\arabic{section}.\arabic{subsection}}
\newcommand{\subsectionseccntformat}{\thesection.\arabic{subsection}}
\makeatletter
\renewcommand{\@seccntformat}[1]{%
  \ifcsname #1seccntformat\endcsname
    \csname #1seccntformat\endcsname
  \else
    \csname the#1\endcsname
  \fi
  \quad
}
\makeatother

\begin{document}

\chapter{Simulations}
\section{Examples 1}
\example{first}
\example{second}
\nameref{ex:first} has the same solution as \nameref{ex:second}.

\end{document}

enter image description here

1
  • You are right, this approach is not optimal. But since the thesis is almost finished (except for cross references and typos) I've chosen this way to save myself some typing.
    – Felix Auer
    May 26, 2018 at 7:10
1

Here is a similar solution not using LaTeX3 which uses the \begingroup\def\zzz{\endgroup ...} trick described in Explanations about \begingroup\edef\x{\endgroup. Like Marcel Krüger's solution this uses the current section counter, increases the subsection counter by one and then fully expands the argument to \section. In particular that means that the section title should be expandable (\emph for example, is out).

\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{nameref}

\newcounter{faex@tempa}
\newcommand{\example}{%
  \setcounter{faex@tempa}{\value{subsection}}%
  \stepcounter{faex@tempa}%
  \begingroup
  \edef\zzz{\endgroup
    \noexpand\subsection{Example \arabic{section}.\arabic{faex@tempa}}}%
  \zzz
  \label{ex:\arabic{section}-\arabic{subsection}}}


\begin{document}
\chapter{Simulations}
\section{Examples 1}
\example
\example
\nameref{ex:1-2} has the same solution as \nameref{ex:1-1}.
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .