我怎样才能显示代码框?

我怎样才能显示代码框?

我正在编写一本简单的手册。在其中,我解释了如何设置运行我编写的管道所需的文件和程序。我希望以突出和易于阅读的方式将命令行命令包含在该文件中。

基本上,如何在我的 LaTeX 文档中获得这种效果:

cd ~/foo; mkdir bar

我原本想使用 a\parbox和背景颜色,但我希望有一个类可以简化这个或任何其他技巧。我不需要代码突出显示,我只是需要一种简单的方法来区分命令和散文。

答案1

我建议使用verbatim环境,因为它很直观,并且应该能提供您想要的区别。这是一个简单的例子:

在此处输入图片描述

\documentclass{article}
\begin{document}
I am in the process of writing a simple manual. In it, I explain how to set up the 
files and programs necessary to run a pipeline I have written. I would like to include 
command line commands in that file in a way that makes them stand out and easy to read.

Basically, how can I get this effect in my LaTeX document:
\begin{verbatim}
cd ~/foo; mkdir bar
\end{verbatim}
I was thinking along the lines of a \verb|\parbox| and a background color but I was 
hoping there might be a class that simplifies this or any other tricks. I don't need 
code highlighting, I just need an easy way to differentiate commands from prose.
\end{document}

也许通过提供的功能定义您自己的fancyvrb或者listings也可以是一个选择,正如使用正确的语法打印程序和/或在 LaTeX 中以文本形式编写源代码

答案2

作为verbatim主题的一个变体,该numberedblock包允许您用边际数字标记代码块,并使用 引用它们\ref。根据您的应用程序,这也可能很有用。(见http://ctan.org/pkg/numberedblock

\documentclass{article}
\usepackage{numberedblock}
\begin{document}
I am in the process of writing a simple manual. In it, I explain how to set up the 
files and programs necessary to run a pipeline I have written. I would like to include 
command line commands in that file in a way that makes them stand out and easy to read.

Basically, how can I get this effect in my LaTeX document:

\begin{numVblock}[\nbVlabel{nb:A}]
cd ~/foo; mkdir bar
\end{numVblock}

I was thinking along the lines of a \verb|\parbox| and a background color but I was 
hoping there might be a class that simplifies this or any other tricks. I don't need 
code highlighting, I just need an easy way to differentiate commands from prose.

I can number larger blocks

\begin{numVblock}[\nbVlabel{nb:B}]
      program test
      implicit none
      integer a, x
c$$$$$$$$$$$$$$$$$$$$$$$$$
      a = 0
      x = 1
   10 a = a + x
      if (a .eq. 100) stop
      goto 10
      end
\end{numVblock}

\noindent and can then reference code blocks \ref{nb:A} and \ref{nb:B}.
\end{document}

在此处输入图片描述

相关内容