'iftoggle' 내부에서는 코드가 아무런 효과가 없는 것 같습니다.

'iftoggle' 내부에서는 코드가 아무런 효과가 없는 것 같습니다.

~ 안에이것hyperref나는 의 자동 pdfauthor 메타데이터 처리 메커니즘을 다음과 같이 패치하도록 제안 받았습니다 . 목표는 PDF 메타데이터를 authblk여러 작성자와 함께 작업하는 것입니다.

\documentclass{article}
\usepackage[pdfusetitle]{hyperref}
\usepackage{authblk}
\usepackage{xpatch}

\newtoggle{patchhref}
\toggletrue{patchhref}
%\iftoggle{patchhref}{
    \xpretocmd{\author}{\addhrauthor{#2}}{}{}
    \newif\iffirstauthor
    \firstauthortrue
    \newcommand{\addhrauthor}[1]{%
        \iffirstauthor%
            \newcommand{\hrauthor}{#1}\firstauthorfalse%
        \else%
            \xapptocmd{\hrauthor}{, #1}{}{}%
        \fi
    }
    \AtEndDocument{
        \hypersetup{pdfauthor={\hrauthor}}
    }
%}{
%}

\begin{document}
\title{The title}
\author{Firstname 1 Lastname 1}
\author{Second author}
\affil{First affiliation\\
   \href{mailto:firstname.fastname@affiliation}{firstname.fastname@affiliation}
}
\author{Name3}
\affil{Second affiliation}

\maketitle
Content.

\end{document}

나는 그것을 추가했는데 iftoggle원래 답변에는 없었습니다. 을 사용하지 않을 때는 iftoggle작동합니다. 그러나 사용 시 iftoggle(관련 세 줄의 주석 처리를 제거하여 시도해 보세요) 로 문서 끝에 도달하면 실패합니다 Undefined control sequence. <argument> \hrauthor. 마치 정의한 명령이 hrauthor실행되지 않은 것처럼 보입니다. 그러나 부분은 \AtEndDocument실행됩니다. 에 따르면이것주석, iftoggle레거시 if구성으로 대체 작동합니다.

iftoggle을 사용하여 이 패치가 조건부로 작동하도록 하려면 어떻게 해야 합니까?우선의조건부 토글 메커니즘?

답변1

\iftoggle이는 그 자체로는 아무 관련이 없지만 카테고리 코드(catcodes)와 관련이 있습니다. TeX가 처음으로 토큰을 스캔할 때 해당 범주 코드는 "동결"됩니다. 즉 TeX는 처음 읽었을 때의 내용을 기억합니다. 귀하의 경우 #범인은 다음과 같습니다.

\iftoggle{patchhref}\@firstoftwoor 와 동등한 것으로 확장됩니다 \@secondoftwo.

\newcommand\@firstoftwo[2]{#1}
\newcommand\@secondoftwo[2]{#2}

이렇게 하면 다음 두 그룹을 검사하고 두 번째 그룹을 제거합니다. 첫 번째 그룹의 모든 catcode는 이 과정에서 고정됩니다. #일반적으로 카테고리 코드가 있으며 6로 명령을 패치할 때는 약간의 주의가 필요합니다 #. \xpretocmd이를 처리하려고 시도하지만 #이미 스캔된 경우 실패합니다.

다음을 정의하여 이 문제를 해결할 수 있습니다.

{\catcode`#=11\relax
    \gdef\fixauthor{\xpretocmd{\author}{\addhrauthor{#2}}{}{}}%
}

앞에 \iftoggle\xpatchcmd\fixauthor.

답변2

토글 부분에서는 정의할 필요가 없습니다 \addrauthor. 어쨌든 코드를 작성해야 합니다.

명령 인수에서 패치를 수행할 때 발생하는 문제는 다양한 방법으로 해결될 수 있으며, 가장 간단한 방법은 표준 조건을 사용하는 것입니다.

여기서는 \addhrauthor.expl3

\documentclass{article}

\usepackage{xpatch,xparse}
\usepackage[pdfusetitle]{hyperref}
\usepackage{authblk}

\ExplSyntaxOn
\seq_new:N \g_oc_hrauthor_seq
\NewDocumentCommand{\addhrauthor}{m}
 {
  \seq_gput_right:Nn \g_oc_hrauthor_seq { #1 }
 }
\NewExpandableDocumentCommand{\hrauthor}{}
 {
  \seq_use:Nn \g_oc_hrauthor_seq {,~}
 }
\ExplSyntaxOff

\newif\ifpatchhref
%\patchhreftrue

\ifpatchhref
  \xpretocmd{\author}{\addhrauthor{#2}}{}{}
  \AtEndDocument{\hypersetup{pdfauthor={\hrauthor}}}
\fi


\begin{document}
\title{The title}
\author{Firstname 1 Lastname 1}
\author{Second author}
\affil{First affiliation\\
   \href{mailto:firstname.fastname@affiliation}{firstname.fastname@affiliation}
}
\author{Name3}
\affil{Second affiliation}

\maketitle
Content.

\end{document}

관련 정보