매크로 정의에서 \StrSubstitute 사용

매크로 정의에서 \StrSubstitute 사용

\emph다음을 사용하여 인수를 조판 하고 해당 이름으로 레이블을 만드는 매크로를 만들고 싶습니다 . 내 이름에 밑줄이 포함되어 있으므로 패키지 \StrSubstitute에서 사용하려고 했습니다 xstring.

\documentclass{article}

\usepackage{xstring}

\newcommand{\tactic}[1]{
\label{\StrSubstitute{#1}{\_}{}}
\emph{#1}
}

\begin{document}

\tactic{EXISTS_TAC}

\end{document}

하지만 나는 얻는다

! Use of \@xs@StrSubstitute@@ doesn't match its definition.
\kernel@ifnextchar ...d@d =#1\def \reserved@a {#2}
\def \reserved@b {#3}\futu... l.12 
\tactic{EXISTS_TAC}

이 문제를 어떻게 해결할 수 있나요?

답변1

실제로는 반대의 문제가 있습니다. 와 같은 입력은 \emph{EXISTS_TAC}오류를 발생시키지만 \label{EXISTS_TAC}완전히 안전합니다.

\documentclass{article}

\usepackage{xstring}

\newcommand{\tactic}[1]{%
  \emph{\noexpandarg\StrSubstitute{#1}{_}{\_}}\label{#1}%
}

\begin{document}

\tactic{EXISTS_TAC}

It was on page~\pageref{EXISTS_TAC}

\end{document}

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

답변2

LuaLaTeX 기반 솔루션은 다음과 같습니다.

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

\documentclass{article}
\usepackage{luacode} % for 'luacode' env. and '\luastringN' macro
\begin{luacode}
function tactic ( s ) 
   tex.sprint ( "\\label{"..s.."}" )
   tex.sprint ( "\\emph{\\detokenize{"..s.."}}" )
end
\end{luacode}
\newcommand\tactic[1]{\directlua{tactic(\luastringN{#1})}}

\begin{document}
\tactic{EXISTS_TAC} \quad \tactic{_^&@$<>}
\end{document}

답변3

참조 레이블 이름에서 밑줄을 제거할 필요는 없지만 다음과 같이 주장하는 경우:

\documentclass{article}

\usepackage{xstring}
\usepackage{textcomp}

\newcommand{\tactic}[1]{%
  \begingroup
  \let\textunderscore=\relax
  \StrSubstitute{#1}{_}{\textunderscore}[\mytemp]%
  \expandafter\endgroup
  \expandafter\emph
  \expandafter{\mytemp}%
  \begingroup
  \StrSubstitute{#1}{_}{}[\mytemp]%
  \expandafter\endgroup
  \expandafter\label
  \expandafter{\mytemp}%
}%

\begin{document}

\tactic{EXISTS_TAC}  \pageref{EXISTSTAC}

\end{document}

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

사용 시 주의할 점하이퍼레프-package는 \label하이퍼링크에 대한 앵커를 배치하지 않으며 \tactic-명령은 어떤 경우에도 카운터를 변경하지 않으므로 또는 또는를 \refstepcounter통해 레이블을 참조하면 섹션화의 상위 항목을 참조하게 됩니다. 해당 명령의 출력이 발생하는 페이지의 페이지 번호를 전달합니다 .\ref\nameref\autoref\pageref\tactic

관련 정보