着色像逐字环境?

时间:2010-07-17 19:13:55

标签: latex

这是Pragmatic Bookshelf书中的屏幕截图。

http://img13.imageshack.us/img13/3168/screenshot20100717at123.png

看起来像着色的逐字环境,但我不确定。

如何显示逐字逐句使用颜色?

使用逐行方法是一种解决方案,但我希望有更好的方法。

{\color{red}\begin{verbatim}
line 1
     line 2
\end{verbatim} }
\color{blue}{\begin{verbatim}
line 3
     line 4
\end{verbatim}}

3 个答案:

答案 0 :(得分:18)

使用fancyvbr软件包的示例

\documentclass[a4paper,10pt]{scrartcl}
\usepackage{color}
\usepackage[utf8]{inputenc}
\usepackage{fancyvrb}
\usepackage[usenames,dvipsnames]{xcolor}

\fvset{frame=single,framesep=1mm,fontfamily=courier,fontsize=\scriptsize,numbers=left,framerule=.3mm,numbersep=1mm,commandchars=\\\{\}}

\begin{document}
   \begin{Verbatim}[label={My orange command sample output}]
# \textcolor{Orange}{My orange command}
My black output
that it can be
many lines long
but this specific \textbf{\textcolor{OliveGreen}{word}}
and the following 2
\textbf{\textcolor{red}{lines are important so they}}
\textbf{\textcolor{red}{need to be highlighted and bold}}
   \end{Verbatim}
\end{document}

Sample output of this code

答案 1 :(得分:3)

您可以使用fancyvrb包。它包含着色选项,因此您可以使用多个Verbatim环境块。

答案 2 :(得分:3)

您也可以使用listings包:

\lstinputlisting[language=Ruby]{source_filename.rb}

或者

\begin{lstlisting}
\end{lstlisting}