1

I would like to refer back to a numbered section in the moderncv class using \ref but I am unable to do so.

I read the post titled Using \ref in moderncv class and I was able to get the following:

\documentclass[11pt,a4paper]{moderncv}
\moderncvstyle{banking}
\moderncvcolor{blue}

\usepackage[margin=1in]{geometry}
\usepackage{etoolbox}
\newcounter{secnumber}
\newcommand{\numbersec}{\refstepcounter{secnumber}\thesecnumber~}
\patchcmd{\section}{\sectionstyle{#1}}{\sectionstyle{\numbersec #1}}{}{}

\renewcommand\sectionstyle[1]{{%
  \refstepcounter{secnumber}%
  \sectionfont
  \textcolor{color1}{\thesecnumber.\quad#1}%
}}

\firstname{First Name}
\familyname{Last Name}

\begin{document}

\makecvtitle

\section{A Section}
\label{sec.one}
Text goes here 

\section{Another Section}
\label{sec.two}
Text goes here 

\section{Yet Another Section}
\label{sec.three}

Recall in section \ref{sec.one} that we mentioned ...

\end{document}

This provides the following output:

enter image description here

As we can see, the section number is not appearing in the position where I use the \ref command.

I attempted to solve this issue by reading the post titled Using \ref in moderncv class however I was not successful.

Is it possible to use \ref and refer back to a numbered section which has been labelled in the moderncv class?

Note: I realize this might be an unusual request, however the reason for asking this question is because I have already have CV made using the moderncv class and I need to also write a supplementary document which requires numbered sections. I would like to use the moderncv class (with modifications) for this supplementary document in order to maintain visual and styling consistency between the CV and supplementary document. The reason for requiring referencing in the supplementary document is to avoid repeating information by requesting the reader to refer back to a certain numbered section.

1

The problem is due to the rather convoluted code used to produce the number. If we simplify that code, then the \label commands will work as you would expect them to do. Instead of incrementing the counter inside the \sectionstyle macro, we prepend it to the \section command. This allows the label to be accessed properly whether the \label command is inside the \section{...} command (as I suggested in my comment) or immediately after it (as you would expect it to work.)

\documentclass[11pt,a4paper]{moderncv}
\moderncvstyle{banking}
\moderncvcolor{blue}

\usepackage[margin=1in]{geometry}
\usepackage{etoolbox}
\newcounter{secnumber}
\pretocmd{\section}{\refstepcounter{secnumber}}{}{}
\renewcommand\sectionstyle[1]{{%
  \sectionfont
  \textcolor{color1}{\thesecnumber.\quad#1}%
}}

\firstname{First Name}
\familyname{Last Name}

\begin{document}

\makecvtitle

\section{A Section}
\label{sec.one}
Text goes here 

\section{Another Section}
\label{sec.two}
Text goes here 

\section{Yet Another Section}
\label{sec.three}

Recall in section \ref{sec.one} that we mentioned and in section \ref{sec.two} ... and in section \ref{sec.three} we see

\end{document}

partial output of code

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.