【问题标题】:Coloring the verbatim like environment?像环境一样为逐字着色?
【发布时间】:2010-07-17 19:13:54
【问题描述】:

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

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

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

如何将颜色用于如图所示的逐字环境?

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

{\color{red}\begin{verbatim} 1号线 2号线 \结束{逐字} } \color{蓝色}{\begin{逐字} 3号线 第 4 行 \结束{逐字}}

【问题讨论】:

  • 问题有一半没用,因为你没有使用正确的图片上传功能。

标签: latex


【解决方案1】:

使用 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}

【讨论】:

  • tabular 中似乎不起作用;只要commandchars 设置成这样,换行符就会丢失。
  • 还没有测试过它们中的任何一个,但看起来这里有几个类似的问题和类似的解决方案:tex.stackexchange.com/questions/119842/… 和这里:tex.stackexchange.com/questions/234292/…
  • 请注意,定义commandchars 非常重要。否则它们只会被解释为正常内容。乍一看这个答案,我错过了他们。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    您也可以使用listings 包:

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

    或者

    \begin{lstlisting}
    \end{lstlisting}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多