\newcommand内の#(ハッシュタグ)を無視する

\newcommand内の#(ハッシュタグ)を無視する

私はlyluatexLilyPond を LaTeX に統合するためにパッケージを使用しています。テキスト内で正しいサイズの Figured Bass を簡単に出力するために、次のコマンドを作成しました。

\newcommand{\fig}[1]{%
    \raisebox{-2pt}{%
        \lilypond{%
        #(set-global-staff-size 18)%
        \figures {#1}
        }%
    }%
}

問題は、#(set-global-staff-size 18)LilyPond コマンドであるのに対し、実際には引数ではないことです。コードはコンパイルされますが、無効な引数を毎回スキップするように指示する必要があります。

\detokenizeそして\string効果がないことが証明されました。

LaTeX に無視させながらハッシュタグを LilyPond に渡す方法について何か提案はありますか?

答え1

#がcatcode 12 になって に入る場合\lilypad、次のようになります。

\documentclass{article}
\begin{document}
\newcommand{\fig}[1]{%
    \raisebox{-2pt}{%
        \prelilypond{%
        ##(set-global-staff-size 18)%
        figures {#1}% I removed the backslash for this non-working example.
        }%
    }%
}

\newcommand\prelilypond[1]{\expandafter\lilypond\expandafter{\string#1}}

\def\lilypond#1{#1}

\fig{1}
\end{document}

ここに画像の説明を入力してください

#catcode 6 でなければならない場合は、次のようになります。

\documentclass{article}
\begin{document}
\newcommand{\fig}[1]{%
    \raisebox{-2pt}{%
        \lilypond{%
        ##(set-global-staff-size 18)%
        figures {#1}% I removed the backslash for this non-working example.
        }%
    }%
}

\def\lilypond#1{\string#1}

\fig{1}
\end{document}

当然ですが、これらの例では、\lilypad冗長なものに再定義したので、実際の音楽の例を実行する必要はありませんでした。

答え2

を使用できますが、いくつかの制御シーケンス名を展開不可にする必要があります。が新しいコマンドであること\edefを保証するために、より低レベルのインターフェイスが必要です。\fig

\makeatletter
\@ifdefinable{\fig}{%
  \edef\fig#1{%
    \noexpand\raisebox{-2pt}{%
      \noexpand\lilypond{%
        \string#(set-global-staff-size 18)%
        \noexpand\figures {#1}%
      }%
    }%
  }%
}
\makeatother

関連情報