0

Update: Thanks everyone for your help, looks like I just misunderstood how \nameref works. I didn't realise it printed the entire caption, so as Ulrike commented it looks like the code is simply infinitely/iteratively expanding self-references. I'll need to find another package/command to achieve what I wanted. Sorry for my mistake! I'll accept Simon's answer and close the question.

I'm trying to insert some blocks of code into my LaTeX document (I'm using TeXStudio, in case that's relevant) using the minted package. I'd like to provide some exposition as to how these different files relate to each other in their captions, and to do that I refer to the second code block in the first code block's caption, and vice versa (i.e. there's a circle of references). I'm using an additional environment ("code") just so that long code breaks across multiple pages.

When I use the \ref command (which just gives the code block's/listing's number), everything works as expected. MWE of working code:

\documentclass{article}
\usepackage{minted}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}


\newenvironment{code}{\captionsetup{type=listing}}{}

\begin{document}


\begin{code}
    \captionof{listing}{Some reference to \ref{code:secondcode}.}
    \label{code:firstcode}
\end{code}

\begin{code}
    \captionof{listing}{Some reference to \ref{code:firstcode}.}
    \label{code:secondcode}
\end{code}

\end{document}

However, if I swap \ref out for \nameref, an error is thrown at compilation time: line 15: TeX capacity exceeded, sorry [grouping levels=255]. ...e reference to \nameref{code:secondcode}.}

MWE of non-working code:

\documentclass{article}
\usepackage{minted}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}


\newenvironment{code}{\captionsetup{type=listing}}{}

\begin{document}


\begin{code}
    \captionof{listing}{Some reference to \nameref{code:secondcode}.}
    \label{code:firstcode}
\end{code}

\begin{code}
    \captionof{listing}{Some reference to \nameref{code:firstcode}.}
    \label{code:secondcode}
\end{code}

\end{document}

I'm happy to work around this somehow, but I'm not sure how, and solving the problem in the nameref subpackage is also beyond my LaTeX skills. I'm not sure whether it's in any way related to the caption or listing packages.

Any suggestions?

EDIT: Forgot to say - the code also works fine when either one of the \namerefs is removed, so it's somehow related to the circular referencing.

simpler example, just loading nameref

\documentclass{article}

\usepackage{nameref}

\begin{document}


\begin{figure}
    \caption{Some reference to \nameref{code:secondcode}.}
    \label{code:firstcode}
\end{figure}

\begin{figure}
    \caption{Some reference to \nameref{code:firstcode}.}
    \label{code:secondcode}
\end{figure}

\end{document}
5
  • Simon's answer probably avoids the probem by using cleveref functionality but the problem is fundamentally with namref, I'll add a simplified example to your question Dec 26, 2021 at 22:01
  • 1
    well you have a circular definition. \nameref{code:secondcode} gives Some reference to \nameref{code:firstcode}. which expands to Some reference to Some reference to \nameref{code:secondcode}. which expands to Some reference to Some reference to Some reference to \nameref{code:firstcode}. and so on. It is not quite clear what you actually want from such a nameref. Dec 26, 2021 at 22:13
  • @Ulrike In my original code, nameref returns the filename, not the caption. So I'm not sure it expands to include the \nameref again. But I may have misunderstood how this works.
    – JoelDR
    Dec 26, 2021 at 22:23
  • @JoelDR if you remove one of the namerefs from the example as posted you will see it generates the caption text, so a circular reference can't be resolved. If you have a setup where it is generating something else, can you modify the example to show that Dec 26, 2021 at 22:26
  • The user guide of the nameref package is clear that \nameref is to be used on \section-type directives and, more specifically, that it is supposed to retrieve the text of the sectioning directive. \nameref is not intended to be used to retrieve the number of some random item you wish to cross-reference. Use other directives, such as \autoref (from the hyperref package) and \cref and friends (from the cleveref package), for such a use case.
    – Mico
    Dec 27, 2021 at 1:47

1 Answer 1

1

Use \Cref instead of \nameref.

\documentclass{article}
\usepackage{minted}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}


\newenvironment{code}{\captionsetup{type=listing}}{}

\begin{document}
    
    
    \begin{code}
        \captionof{listing}{Some reference to \Cref{code:secondcode}.}
        \label{code:firstcode}
    \end{code}
    
    \begin{code}
        \captionof{listing}{Some reference to \Cref{code:firstcode}.}
        \label{code:secondcode}
    \end{code}
    
\end{document}

a

1
  • 1
    well this doesn't give the \nameref (which would use the text "Some reference to ...".) Dec 26, 2021 at 22:08

You must log in to answer this question.

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