![ネストされた中括弧を含む LaTeX \newenvironment](https://rvso.com/image/281509/%E3%83%8D%E3%82%B9%E3%83%88%E3%81%95%E3%82%8C%E3%81%9F%E4%B8%AD%E6%8B%AC%E5%BC%A7%E3%82%92%E5%90%AB%E3%82%80%20LaTeX%20%5Cnewenvironment.png)
私は Beamer プレゼンテーションを作成しており、ターミナル スタイルのボックスにターミナル コマンドを表示したいと考えていました。tkiz を使用して必要なものを作成しましたが、プリプロセッサの括弧が一致していないと思われるため、新しい環境として定義するのに問題があります。環境に変換したいブロックは次のとおりです。
% 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}
結果は次のとおりです。
しかし、新しい環境にしようとすると、プリプロセッサが括弧#2と一致してエラーが発生します。パッケージを使用してみました環境そのようです:
\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}%
}
しかし、まだエラーが発生します。この環境でネストされた中括弧をエスケープする方法はありますか?
また、ここに前文があります:
\documentclass{article}
\usepackage{tikz}
\usepackage{listings}
\usepackage{lipsum}
\usepackage{courier}
\usepackage{environ}
\usetikzlibrary{shapes}
\lstset{basicstyle=\ttfamily,breaklines=true}
答え1
lstlisting
で定義された環境内に隠れることは\NewEnviron
、実際には機能しません。lstlisting
これは、コードがすでに に吸収されている場合には環境が実行できない方法でコードを調整する必要があるためです\BODY
。
動作するバージョンは次のとおりです (ほぼ動作していますが、サイズの問題を解決する必要があります)。
\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}
より適切な LaTeX スタイルとしては を使用しますlrbox
が、 では\lstnewenvironment
それが適切ではないことが判明しました。
パッケージも確認してくださいtcolorbox
。
答え2
回答ありがとうございます、egreg さん。あなたの解決策と graphicx パッケージの resizebox を使用することで、サイズの問題を解決し、幅を textwidth と同じにしました。これを使いたい人のためにここに投稿します。また、tcolorbox で同じことを行う方が簡単なはずですが、これには 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}