我正在制作一个 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,我解决了尺寸问题,并使宽度等于文本宽度。在此发布,供任何想要使用的人使用。此外,使用 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}