LaTeX \newenvironment com chaves aninhadas

LaTeX \newenvironment com chaves aninhadas

Estou fazendo uma apresentação no beamer e queria mostrar os comandos do terminal em uma caixa estilo terminal. Eu criei o que quero usando tkiz, mas agora tenho alguns problemas para defini-lo como um novo ambiente devido ao que considero serem chaves incompatíveis do pré-processador. Aqui está o bloco que quero transformar em ambiente.

% Define box and box title style
\tikzstyle{terminal} = [draw=white, text=white, font=courier, fill=black, very thick,
    rectangle, inner sep=10pt, inner ysep=20pt]
\tikzstyle{terminalTitle} =[fill=black, text=white, draw=white]

    \begin{tikzpicture}
    \node [terminal] (box){      % 1: this
    \begin{minipage}{0.90\textwidth}
    \begin{lstlisting}
    $ python -c " print 'x'*80 + '\x01' " | ./test1
    Enter password:
    You win!
    $
    \end{lstlisting}
    \end{minipage}
    };                           % 2: matches this
    \node[terminalTitle, rounded corners, right=10pt] at (box.north west) {\texttt{tty1: /bin/bash}};
    \end{tikzpicture}

Aqui está o resultado:

insira a descrição da imagem aqui

Mas quando tento entrar em um novo ambiente, o pré-processador corresponde à chave 2 e gera erros. Eu tentei usar o pacotemeio ambienteigual a:

\NewEnviron{terminal}{
\tikzstyle{terminal} = [draw=white, text=white, font=courier, fill=black, very thick,
    rectangle, inner sep=10pt, inner ysep=20pt]
\tikzstyle{terminalTitle} =[fill=black, text=white, font=\ttfamily, draw=white]

\begin{tikzpicture}
\node [terminal] (box){%
    \begin{minipage}{0.90\textwidth}
\begin{lstlisting}
\BODY
\end{lstlisting}
    \end{minipage}
};
\node[terminalTitle, rounded corners, right=10pt] at (box.north west) {tty: /bin/bash}};
\end{tikzpicture}%
}

Mas ainda recebo erros. Existe uma maneira de escapar das chaves aninhadas neste ambiente?

Também aqui está o preâmbulo:

\documentclass{article}

\usepackage{tikz}
\usepackage{listings}
\usepackage{lipsum}
\usepackage{courier}
\usepackage{environ}

\usetikzlibrary{shapes}

\lstset{basicstyle=\ttfamily,breaklines=true}

Responder1

Esconder-se lstlistingdentro de um ambiente definido com \NewEnvironnão pode realmente funcionar, porque o lstlistingambiente precisa massagear o código de uma maneira que não seria possível se o código já tivesse sido absorvido pelo \BODY.

Aqui está uma versão funcional (quase, você precisa resolver o problema de tamanho):

\documentclass{article}

\usepackage{tikz}
\usepackage{listings}

\usetikzlibrary{shapes}

\lstset{basicstyle=\ttfamily,breaklines=true}
\newsavebox\terminalbox
\lstnewenvironment{terminal}[1][]
  {\lstset{#1}\setbox\terminalbox=\vbox\bgroup\hsize=0.7\textwidth}
  {\egroup
   \tikzstyle{terminal} = [
    draw=white, text=white, font=courier, fill=black, very thick,
    rectangle, inner sep=10pt, inner ysep=20pt
   ]
   \tikzstyle{terminalTitle} = [
     fill=black, text=white, font=\ttfamily, draw=white
   ]
   \begin{tikzpicture}
   \node [terminal] (box){\usebox{\terminalbox}};
   \node[terminalTitle, rounded corners, right=10pt] at (box.north west) {tty: /bin/bash};
   \end{tikzpicture}
}

\begin{document}

\begin{terminal}
$ python -c " print 'x'*80 + '\x01' " | ./test1
Enter password:
You win!
$
\end{terminal}

\end{document}

Um estilo LaTeX melhor usaria lrbox, mas acontece que ele \lstnewenvironmentnão gosta.

insira a descrição da imagem aqui

Verifique também o tcolorboxpacote.

Responder2

Obrigado egreg pela sua resposta. Com base na sua solução e usando o resizebox do pacote graphicx, resolvi o problema do tamanho e tornei a largura igual à largura do texto. Postando aqui para quem quiser usar isso. Além disso, fazer o mesmo com o tcolorbox deve ser mais fácil, mas não requer nenhum pacote CTAN.

\usepackage{graphicx}
\usepackage{listings}
\usepackage{color}
\usepackage{courier}
\usepackage{tikz}

\usetikzlibrary{shapes}

\lstset{basicstyle=\ttfamily\footnotesize,breaklines=true}
\newsavebox\terminalbox
\lstnewenvironment{terminal}[1][]
  {\lstset{#1}\setbox\terminalbox=\vbox\bgroup\hsize=0.8\textwidth}
  {\egroup
   \tikzstyle{terminal} = [
    draw=white, text=white, font=courier, fill=black, very thick,
    rectangle, inner sep=2pt, inner ysep=8pt
   ]
   \tikzstyle{terminalTitle} = [
     fill=black, text=white, font=\ttfamily, draw=white
   ]
   \noindent\resizebox{\textwidth}{!}{ % This line fits the box to textwidth
   \begin{tikzpicture}
   \node [terminal] (box){\usebox{\terminalbox}};
   \node[terminalTitle, rounded corners, right=10pt] at (box.north west) {tty: /bin/bash};
   \end{tikzpicture}}
}

\begin{document}
    \begin{terminal}
    $ python -c " print 'x'*80 + '\x01' " | ./test1
    Enter password:
    You win!
    $
    \end{terminal}
\end{document}

insira a descrição da imagem aqui

informação relacionada