자동 번호가 매겨진 단락을 참조할 때 사용자 정의된 출력

자동 번호가 매겨진 단락을 참조할 때 사용자 정의된 출력

기술을 정의하고 자동 번호를 매기는 데 사용하는 사용자 정의 단락이 있습니다. 단락을 참조할 때 출력은 사용자 정의 텍스트와 카운터 번호(예: 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}

여기에 이미지 설명을 입력하세요

관련 정보