忽略 \newcommand 中的 #(主題標籤)

忽略 \newcommand 中的 #(主題標籤)

我正在使用該lyluatex包將 LilyPond 整合到 LaTeX 中。我創建了以下命令,以便在文字中輕鬆輸出正確大小的圖形低音:

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

問題是#(set-global-staff-size 18)實際上並不是一個參數,而是一個 LilyPond 指令。程式碼確實可以編譯,但我必須告訴它每次都跳過無效參數。

\detokenize並被\string證明是無效的。

關於如何將主題標籤傳遞給 LilyPond 同時讓 LaTeX 忽略它,有什麼建議嗎?

答案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}

在此輸入影像描述

如果#必須是類別代碼 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

相關內容