我有一个自定义段落,用于定义技能并自动编号。引用段落时,输出应为自定义文本加上计数器编号(例如 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
它的定义方式只能用于后创建\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}