![帶有嵌套大括號的 LaTeX \newenvironment](https://rvso.com/image/281509/%E5%B8%B6%E6%9C%89%E5%B5%8C%E5%A5%97%E5%A4%A7%E6%8B%AC%E8%99%9F%E7%9A%84%20LaTeX%20%5Cnewenvironment.png)
我正在製作投影機演示文稿,我想在終端樣式的框中顯示終端命令。我已經使用 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}