マクロ定義で \StrSubstitute を使用する

マクロ定義で \StrSubstitute を使用する

引数をタイプセットし、その名前のラベルも作成するマクロを作成したいと思います。名前にアンダースコアが含まれているため、パッケージから\emph使用しようとしました。\StrSubstitutexstring

\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いずれの場合でも -command は を介し​​てカウンターを変更しないため、またはを\refstepcounter介し​​てラベルを参照すると、セクションの上位項目が参照されることになります。は、対応する -command の出力が発生するページのページ番号を提供します。\ref\nameref\autoref\pageref\tactic

関連情報