用多个参数替换嵌套的 LaTeX 命令

用多个参数替换嵌套的 LaTeX 命令

我想\teilseiten{left col width}{left col text}{right col text}用嵌套环境替换 LaTeX 命令。问题是,第二和第三个参数\teilseiten包含命令和/或环境。

\teilseiten{0.5}{
  Some text and \textbf{even}
  \begin{tiny}
     environments
  \end{tiny}
}{
  Some other text and \textbf{even}
  \begin{tiny}
    more environments
  \end{tiny}
}

应更换为

\begin{mycolumns}
  \begin{mycolumn}{0.5}
    Some text and \textbf{even}
    \begin{tiny}
       environments
    \end{tiny}
  \end{mycolumn}
  \begin{mycolumn}{0.5}
    Some other text and \textbf{even}
    \begin{tiny}
       more environments
    \end{tiny}
  \end{mycolumn}
\end{mycolumns}

有人知道如何实现这一点吗?

答案1

我假设您有一个可用的标准 *nix Bash 环境。

#!/bin/sed -f

# Match pattern that begins ("^") with "\teil...", capture what's
# between braces thru parentheses.
/^\\teilseiten\({.*}\){/ {
# And...

    # Copy pattern on hold space ("h"); substitute pattern with new
    # lines, with captured text at the end, retrieved by backreference
    # ("\1"); process next line ("n").
    h; s//\\begin{mycolumns}\n  \\begin{mycolumn}\1/; n

    # Set a label.
    : loop

    # Add two spaces at the beginning of line ("^"); process next line 
    # ("n").
    s/^/  /; n

    # If line contains "\end{tiny}" add two spaces in front, a newline
    # ("\n") at the end of it ("&"), and the new line " \end{my...".
    s/.*\\end{tiny}/  &\n  \\end{mycolumn}/

    # If no substitution was succesful after last read line ("n")
    # (i.e. the "s" above wasn't triggered), jump to label.
    T loop

    # Process next line ("n"); if braces "}{" are at beginning of line
    # ("^"), then copy ("g") hold space to pattern space (see "h"
    # above); capture content between braces, substitute pattern with
    # line "\begin{my..." and captured string "\1" at the end.
    n; /^}{/ {
        g; s/.*\({.*}\){/\\begin{mycolumn}\1/
    }

    # If the above substitution was successful (i.e. "}{" was
    # matched), jump to label.
    t loop

    # Substitute brace with "\end{my...".
    s/^}$/\\end{mycolumns}/
}

将脚本粘贴到lanext.sh文件中,并通过以下方式使其可执行:

chmod +x lanext.sh

按如下方式运行:

./lanext.sh test.txt

test.txt需要替换的代码在哪里。

相关内容