我有一個自訂段落,用於定義技能並自動編號。引用段落時,輸出應為自訂文字加計數器編號(例如,S1、S2 等)。但目前僅列印數字。
我的方法做錯了什麼?
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage[english]{babel}
\newcounter{skillCounter}
\newcommand{\skillParagraph}[2]{%
\refstepcounter{skillCounter}%
\paragraph*{#1 (S\arabic{skillCounter})}%
\label{#2}%
\expandafter\def\csname customlabel@#2\endcsname{#1 (S\theskillCounter)}%
}
% Custom command to reference custom labels
\newcommand{\cparref}[1]{%
\hyperref[#1]{\csname customlabel@#1\endcsname}%
}
\begin{document}
\section*{Skills}
\skillParagraph{Maths}{skill:maths} You should know maths.
\skillParagraph{Latex}{skill:latex} You should know \LaTeX.
You need skills \ref{skill:maths} and \ref{skill:latex}.
\skillParagraph{Word}{skill:word} You should know Word.
You need skills \ref{skill:maths}, \ref{skill:latex}, and \ref{skill:word}.
\end{document}
答案1
您應該定義計數器的表示。最好將 \refstepcounter 放在段落內,這樣錨點就不能與標題分開。
\documentclass{article}
\usepackage{hyperref}
\usepackage[english]{babel}
\newcounter{skillCounter}
\renewcommand\theskillCounter{S\arabic{skillCounter}}
\makeatletter
\newcommand{\skillParagraph}[2]{%%
\paragraph*{%
\refstepcounter{skillCounter}%
\protected@edef\@currentlabelname{#1 (\theskillCounter)}%
\label{#2}%
#1 (\theskillCounter)}%%
}
\makeatother
% Custom command to reference custom labels
\newcommand{\cparref}[1]{\nameref{#1}}
\begin{document}
\section*{Skills}
\skillParagraph{Maths}{skill:maths} You should know maths.
\skillParagraph{Latex}{skill:latex} You should know \LaTeX.
You need skills \ref{skill:maths} and \ref{skill:latex}.
\skillParagraph{Word}{skill:word} You should know Word.
You need skills \ref{skill:maths}, \ref{skill:latex}, and \ref{skill:word}.
\nameref{skill:maths}
\end{document}
答案2
我的方法做錯了什麼?
我想說兩件事。首先,您所描述的預期結果顯然應該是您的自訂參考命令的結果\cparref
,但是您隨後使用標準引用了所有內容\ref
,當然,正如預期的那樣,標準只是引用了數字。其次,也是更基本的,\cparref
是以只能使用的方式定義的後創建標籤的the \skillParagraph
,這並不完全是交叉引用(通常)應該做的事情。此外,您\cparref
似乎只是在重新發明輪子nameref
,那麼為什麼不直接使用您已有的呢?
\documentclass{article}
% utf8 has been the default inputenc for some years now
% \usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage[english]{babel}
\GetTitleStringSetup{expand}
\newcounter{skillCounter}
% You could instead hard-code the S into \skillParagraph, as you've been
% doing, or define an \autoref prefix, etc. This just seems to get a
% reasonable default value for \ref (affects any cross-reference to the
% counter though).
\renewcommand{\theskillCounter}{S\arabic{skillCounter}}
\newcommand{\skillParagraph}[2]{%
\refstepcounter{skillCounter}%
\paragraph*{#1 (\theskillCounter)}%
\label{#2}%
}
\begin{document}
\section*{Skills}
\skillParagraph{Maths}{skill:maths} You should know maths.
\skillParagraph{Latex}{skill:latex} You should know \LaTeX.
You need skills \ref{skill:maths} and \ref{skill:latex}.
Note that \ref{skill:word} and \nameref{skill:word} work before the
corresponding skill paragraph.
\skillParagraph{Word}{skill:word} You should know Word.
You need skills \ref{skill:maths}, \ref{skill:latex}, and \ref{skill:word}.
And, for whatever you wanted \texttt{\textbackslash{}cparref} to do, just use
\texttt{\textbackslash{}nameref}: \nameref{skill:maths},
\nameref{skill:latex}, and \nameref{skill:word}.
\end{document}