![マクロ定義で \StrSubstitute を使用する](https://rvso.com/image/391703/%E3%83%9E%E3%82%AF%E3%83%AD%E5%AE%9A%E7%BE%A9%E3%81%A7%20%5CStrSubstitute%20%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8B.png)
引数をタイプセットし、その名前のラベルも作成するマクロを作成したいと思います。名前にアンダースコアが含まれているため、パッケージから\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
答え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