Substituir um comando LaTeX aninhado por múltiplos argumentos

2

Gostaria de substituir o comando LaTeX \teilseiten{left col width}{left col text}{right col text} por um ambiente aninhado. O problema é que o segundo e terceiro argumento de \teilseiten contém comandos e / ou ambientes.

\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}
}

Deve ser substituído com

\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}

Alguém sabe como conseguir isso?

    
por Simon Schälli 05.11.2015 / 17:37

1 resposta

1

Eu suponho que você tenha um ambiente Bash padrão * nix disponível.

#!/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
    # (""); process next line ("n").
    h; s//\begin{mycolumns}\n  \begin{mycolumn}/; 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 "" at the end.
    n; /^}{/ {
        g; s/.*\({.*}\){/\begin{mycolumn}/
    }

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

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

Cole o script no arquivo lanext.sh e torne-o executável por:

chmod +x lanext.sh

Execute desta forma:

./lanext.sh test.txt

Em que test.txt detém o código que precisa ser substituído.

    
por 06.11.2015 / 22:16