섹션 제목에서 하이퍼참조와 함께 \newrobustcmd 및 \NewDocumentCommand를 사용하는 방법

섹션 제목에서 하이퍼참조와 함께 \newrobustcmd 및 \NewDocumentCommand를 사용하는 방법

활성화된 섹션 제목에 어떻게 사용할 수 \newrobustcmd있나요 ? 아래 MWE는 렌더링된 PDF가 올바르게 보이지만 책갈피에 매크로 내용이 표시되지 않음을 보여줍니다.NewDocumentCommandhyperref

\documentclass[11pt]{article}

\usepackage{hyperref}
\usepackage{xparse}
\usepackage{etoolbox}

\newcommand{\testA}[0]{world}
\newrobustcmd{\testB}[0]{world}
\NewDocumentCommand{\testC}{}{world}

\begin{document}
    \section{Hello \testA}
    \section{Hello \testB}
    \section{Hello \testC}
\end{document}

결과 PDF

파일 MWE.out은 다음과 같습니다.

\BOOKMARK [1][-]{section.1}{Hello world}{}% 1
\BOOKMARK [1][-]{section.2}{Hello }{}% 2
\BOOKMARK [1][-]{section.3}{Hello }{}% 3

답변1

를 사용 하면 \newcommand확장 가능한 매크로( \def)를 정의하고 을 사용하면 \newrobustcmd엔진 \NewDocumentCommand보호 매크로( \protected\def)를 정의할 수 있습니다. 북마크 문자열이 작성되는 동안 북마크 내용이 확장되지만 보호된 매크로는 확장될 수 없으므로 토큰 \testB\testCMWE의 토큰은 북마크에 있는 대로 종료됩니다. hyperref그것들을 어떻게 해야할지 모르기 때문에 그것들을 버립니다.

Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref)                removing `\testB' on input line 13.


Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref)                removing `\testC' on input line 14.

따라서 보호된 매크로가 정말로 필요한 경우 두 가지 선택이 있습니다.

  • \section{Hello \texorpdfstring{\testB}{world}}문서에 사용하거나
  • hyperref북마크에 사용될 로드 후 서문에 더 간단한 확장 가능한 정의를 추가하세요.

    \pdfstringdefDisableCommands{%
      \def\testB{world}%
      \def\testC{wor‌​ld}%
    }
    

관련 정보