我正在尝试在 LaTeX 中生成类似以下内容 - 只是总体布局,说明两个命令行会话之间的通信流程:
我尝试过以下变化:
\documentclass{article}
\usepackage{listings}
\begin{document}
\subsubsection{Named pipes}
A named pipe has a p at the start of the line:
\begin{lstlisting}
root@vogon:~# mkfifo pipe
root@vogon:~# ls -l pipe
prw-r--r-- 1 root root 0 Aug 17 09:49 pipe
\end{lstlisting}
As illustrated, named pipes are created with mkfifo - they can be used to communicate between processes; if a process opens the named pipe for reading, it will wait until another process writes into the pipe. The following shows how named pipes work, with two terminals open - first create the pipe in the left window and read from it with cat pipe - it just hangs, waiting for something to arrive; next write to the same pipe in the right window with echo "This is a test" > pipe - now the left window prints out the text string, and cat terminates:
\begin{tabular}[t]{l|l}
\begin{lstlisting}
root@vogon:~# mkfifo pipe
root@vogon:~# ls -l pipe
prw-r--r-- 1 root root 0 Aug 17 09:49 pipe
root@vogon:~# cat pipe
... waiting ...
\end{lstlisting} & \\
& \begin{lstlisting}
root@vogon:~# echo "This is a test" > pipe
\end{lstlisting} \\
\begin{lstlisting}
root@vogon:~# cat pipe
This is a test
\end{lstlisting} & \\
\end{tabular}
\end{document}
这会产生非常可怕的结果,代码被挤到页面的右侧,文本行从右侧伸出(或者说被切断)。我觉得一定有一个简单的方法可以做到这一点,但我太沮丧了,无法清晰地思考 - 最好的方法是什么?
答案1
有多种问题会导致观察到的行为。
首先,在源代码中,您已将终端输出缩进 8 个空格。此缩进会打印在文档中,因此会导致块向右移动。
您可以通过不缩进源代码来解决这个问题,即
\begin{tabular}[t]{ll}
Terminal 1 & Terminal 2\\[3mm]
\begin{lstlisting}
root@vogon:~# mkfifo pipe
root@vogon:~# ls -l pipe
prw-r--r-- 1 root root 0 Aug 17 09:49 pipe
root@vogon:~# cat pipe
... waiting ...
\end{lstlisting}
或者通过使用gobble
列表中的键,从源中删除预定义数量的字符:
\begin{lstlisting}[gobble=8]
或者通过使用lstautogobble
提供新列表键的包autogobble
来自动删除所有空格。
\usepackage{lstautogobble}
% ...
\begin{lstlisting}[autogobble]
其次,表格位于新段落的第一行,因此表格将缩进。您可以使用以下命令关闭该功能\noindent
:
prints out the text string, and cat terminates:
\noindent
\begin{tabular}[t]{ll}
或者不开始新的段落:
prints out the text string, and cat terminates:
\begin{tabular}[t]{ll}
第三,列表太宽。您可以将linewidth
和 一起设置breaklines
,并可选地设置一个较小的breakindent
和 后中断符号:
\lstset{%
breaklines,
linewidth=0.4\textwidth,
postbreak=\mbox{\textcolor{blue}{$\hookrightarrow$}\space},
breakindent=5pt,
% ...
另外默认字体太大,您也可以设置:
\lstset{%
basicstyle=\scriptsize\ttfamily,
% ...
最后,在每个终端周围放置某种框架看起来更好:
\lstset{%
frame=shadowbox,
rulesepcolor=\color{gray!45},
% ...
完整代码:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{lstautogobble}
\usepackage{xcolor}
\begin{document}
\subsubsection{Named pipes}
A named pipe has a p at the start of the line:
\lstset{%
basicstyle=\scriptsize\ttfamily,
autogobble,
breaklines,
linewidth=0.4\textwidth,
postbreak=\mbox{\textcolor{blue}{$\hookrightarrow$}\space},
breakindent=5pt,
frame=shadowbox,
rulesepcolor=\color{gray!45}
}
\begin{lstlisting}[linewidth=\textwidth]
root@vogon:~# mkfifo pipe
root@vogon:~# ls -l pipe
prw-r--r-- 1 root root 0 Aug 17 09:49 pipe
\end{lstlisting}
As illustrated, named pipes are created with mkfifo - they can be used to communicate between processes; if a process opens the named pipe for reading, it will wait until another process writes into the pipe. The following shows how named pipes work, with two terminals open - first create the pipe in the left window and read from it with cat pipe - it just hangs, waiting for something to arrive; next write to the same pipe in the right window with echo "This is a test" > pipe - now the left window prints out the text string, and cat terminates:
\noindent\rule{\linewidth}{1pt}
\begin{tabular}[t]{ll}
Terminal 1 & Terminal 2\\[3mm]
\begin{lstlisting}
root@vogon:~# mkfifo pipe
root@vogon:~# ls -l pipe
prw-r--r-- 1 root root 0 Aug 17 09:49 pipe
root@vogon:~# cat pipe
... waiting ...
\end{lstlisting} & \\
& \begin{lstlisting}
root@vogon:~# echo "This is a test" > pipe
\end{lstlisting} \\
\begin{lstlisting}
root@vogon:~# cat pipe
This is a test
\end{lstlisting} & \\[3mm]
\end{tabular}
\rule{\linewidth}{1pt}
\end{document}
请注意,我添加了\usepackage[T1]{fontenc}
以确保>
字符在 pdfLaTeX 中正确打印。
还要注意,阴影盒有一点点悬垂,我不确定该怎么办,但我认为它比非盒装版本更好。
结果: