是否可以運行以文字形式儲存在變數中的命令?

是否可以運行以文字形式儲存在變數中的命令?

我有一些複雜的表達式,其中包含大量難以使用的嵌套命令。為了解決這個問題,我使用 pgfparser 編寫了一個解析器,它為我創建了表達式。然後我可以將輸出複製回 LaTeX,它會輸出我需要的內容。

我想知道是否可以自動化執行此操作。我可以將文字(一系列命令)儲存在變數中,但我找不到在變數中運行命令的方法

解析器本身並不是很有趣,但作為一個 mwe,這是我所擁有的簡化版本:

\documentclass{standalone}
\usepackage[T1]{fontenc}
\usepackage{pgf}
\usepgfmodule{parser}

\pgfparserdef{mweparser}{initial}{\meaning [}{\textbackslash overline\{}
\pgfparserdef{mweparser}{initial}{\meaning ]}{\}}
\pgfparserdef{mweparser}{initial}{\meaning A}{A}
\pgfparserdef{mweparser}{initial}{subscript character _}{\_\{\pgfparserswitch{subscript}}
\pgfparserdef{mweparser}{subscript}{\meaning +}{\textbackslash hat\{\pgfparserswitch{edgename}}
\pgfparserdef{mweparser}{subscript}{\meaning -}{\textbackslash check\{\pgfparserswitch{edgename}}
\pgfparserdef{mweparser}{edgename}{\meaning a}{a\}\pgfparserswitch{subscript}}
\pgfparserdef{mweparser}{edgename}{\meaning b}{b\}\pgfparserswitch{subscript}}
\pgfparserdef{mweparser}{subscript}{subscript character _}{\}\pgfparserswitch{initial}}
\pgfparserdef{mweparser}{all}{the character ;}{\pgfparserswitch{final}}

\begin{document}
    \pgfparserparse{mweparser}[A_+a-b-a_[A_+a+b-a_]];%
\end{document}

這將輸出:

\overline{A_{\hat{a}\check{b}\check{a}}\overline{A_{\hat{a}\hat{b}\check{a}}}}

這就是我想要運行的(在數學模式下)。我可以將此文字儲存在變數中,但不知道如何運行它。

我對這些東西很陌生,所以如果我做錯了什麼,我深感抱歉。

答案1

我真的不知道 PGF 解析器是如何工作的,但是你用程式碼得到的是一個不是你想要的字串:\textbackslash不是反斜杠,而是打印反斜杠的命令。

l3regex這是LaTeX3 的正規表示式解析器的實作;已新增所需結果以檢查巨集的作用。

\documentclass[border=3,varwidth]{standalone}
\usepackage[T1]{fontenc}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\NewDocumentCommand{\term}{m}
 {
  \dquick_term:n { #1 }
 }

\tl_new:N \l__dquick_input_tl

\cs_new_protected:Npn \dquick_term:n #1
 {
  \tl_set:Nn \l__dquick_input_tl { #1 }
  %% subscripts (works so long you don't have nested subscripts)
  \regex_replace_all:nnN { \_(.*?)\_ } { \cD\_\cB\{\1\cE\} } \l__dquick_input_tl
  %% [ becomes \overline\bgroup (nesting is allowed)
  \regex_replace_all:nnN { \[ } { \c{overline}\c{bgroup} } \l__dquick_input_tl
  %% ] becomes \egroup
  \regex_replace_all:nnN { \] } { \c{egroup} } \l__dquick_input_tl
  %% +<char> becomes \hat{<char>}
  \regex_replace_all:nnN { \+(.) } { \c{hat}\cB\{\1\cE\} } \l__dquick_input_tl
  %% -<char> becomes \check{<char>}
  \regex_replace_all:nnN { \-(.) } { \c{check}\cB\{\1\cE\} } \l__dquick_input_tl
  $\tl_use:N \l__dquick_input_tl$
 }
\ExplSyntaxOff

\begin{document}
\term{[G_+a-b-c_[H_+a+a-c_]]}

$\overline{G_{\hat{a}\check{b}\check{c}}\overline{H_{\hat{a}\hat{a}\check{c}}}}$
\end{document}

在此輸入影像描述


略有不同的版本,允許為巢狀巨集指定分隔符號(它們必須不同),此處顯示為\overline

\documentclass[border=3,varwidth]{standalone}
\usepackage[T1]{fontenc}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\NewDocumentCommand{\term}{m}
 {
  \dquick_term:n { #1 }
 }

\tl_new:N \l__dquick_input_tl

\cs_new_protected:Npn \dquick_term:n #1
 {
  \tl_set:Nn \l__dquick_input_tl { #1 }
  %% subscripts (works so long you don't have nested subscripts)
  \regex_replace_all:nnN { \_(.*?)\_ } { \cD\_\cB\{\1\cE\} } \l__dquick_input_tl
  %% brackets can be nested, this requires a slower routine
  \dquick_replace_nested:nnn { \[ } { \] } { overline }
  %% +<char> becomes \hat{<char>}
  \regex_replace_all:nnN { \+(.) } { \c{hat}\cB\{\1\cE\} } \l__dquick_input_tl
  %% -<char> becomes \check{<char>}
  \regex_replace_all:nnN { \-(.) } { \c{check}\cB\{\1\cE\} } \l__dquick_input_tl
  \tl_use:N \l__dquick_input_tl
 }

\cs_new_protected:Npn \dquick_replace_nested:nnn #1 #2 #3
 {% #1 is the left delimiter
  % #2 is the right delimiter
  % #3 is the macro name for replacement
  \regex_match:nVT { #1 ( [^#1#2]*? ) #2 } \l__dquick_input_tl
   {
    \regex_replace_all:nnN { #1 ( [^#1#2]*? ) #2 } { \c{#3}\cB\{\1\cE\} } \l__dquick_input_tl
    \dquick_replace_nested:nnn { #1 } { #2 } { #3 }
   }
 }
\cs_generate_variant:Nn \regex_match:nnT { nV }

\ExplSyntaxOff

\begin{document}
$\term{[G_+a-b-c_[H_+a+a-c_]]}$

$\overline{G_{\hat{a}\check{b}\check{c}}\overline{H_{\hat{a}\hat{a}\check{c}}}}$
\end{document}

相關內容