如何处理宏定义的字符串参数中的“#”?如何用其他字符替换该参数中的“#”
\documentclass{article}
\usepackage{xstring}
\newcommand{\notesus}[1]{
\StrSubstitute{#1}{s}{ sustained}
}
\newcommand{\notesharp}[1]{
\noexpandarg
\StrSubstitute{#1}{\#}{ sustained}
}
\begin{document}
The songs is mostly written in \notesus{Cs} \par
Not many songs combine the simultaneous use of flats and sustained notes
particularly \notesus{Fs}\par
But most of the time we use C\# to denote C sustained
I need to use C# without back slash betwen C and # like this \notesharp{C#}
\end{document}
答案1
使用更好的替换功能;此处的内容中的尾随s
或被更改。#
\c_hpcolos_sustained_tl
\documentclass{article}
\usepackage{xparse,l3regex}
\ExplSyntaxOn
\NewDocumentCommand{\notes}{m}
{
\tl_set:Nn \l_hpcolos_notes_tl { #1 }
\regex_replace_once:nnN { [s\#] \Z } { \u{c_hpcolos_sustained_tl} } \l_hpcolos_notes_tl
\tl_use:N \l_hpcolos_notes_tl
}
\tl_new:N \l_hpcolos_notes_tl
\tl_const:Nn \c_hpcolos_sustained_tl { ~ sustained }
\ExplSyntaxOff
\begin{document}
\verb|Cs|: \notes{Cs}
\verb|Fs|: \notes{Fs}
\verb|C#|: \notes{C#}
\end{document}
答案2
将其放置在紧接着的位置\begin{document}
:
\catcode`\#=12
那么,哈希只是文档正文中的一个普通字符。只要您不在正文中创建宏,就可以使用。