使用 \ref 參考 Moderncv 類別中的編號部分

使用 \ref 參考 Moderncv 類別中的編號部分

我想回顧一下moderncv課堂上的編號部分,\ref但我無法這樣做。

我讀了標題為 在moderncv類別中使用\ref 我得到了以下資訊:

\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}

這提供了以下輸出:

在此輸入影像描述

正如我們所看到的,節號沒有出現在我使用該\ref命令的位置。

我試圖透過閱讀標題為的帖子來解決這個問題在moderncv類別中使用\ref 然而我沒有成功。

是否可以使用\ref並參考moderncv班級中已標記的編號部分?

筆記:我意識到這可能是一個不尋常的請求,但是提出這個問題的原因是因為我已經使用該moderncv課程製作了簡歷,我還需要編寫一份需要編號部分的補充文件。我想使用moderncv此補充文件的類別(經過修改),以保持簡歷和補充文件之間的視覺和樣式一致性。要求在補充文件中引用的原因是要求讀者回顧某個編號的部分,以避免重複訊息。

答案1

問題是由於用於產生該數字的程式碼相當複雜。如果我們簡化該程式碼,那麼\label命令將按照您期望的方式運作。我們沒有增加巨集內部的計數器\sectionstyle,而是將其添加到\section命令之前。這允許正確存取標籤,無論該\label命令是在\section{...}命令內部(正如我在評論中建議的那樣)還是緊隨其後(正如您所期望的那樣)。

\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}

程式碼的部分輸出

相關內容