让 fancyvrb 和 mdframed 一起工作

让 fancyvrb 和 mdframed 一起工作

我想将终端输出格式化如下:

  • 黑色背景上白色文字
  • 用户写的命令被强调(倾斜的打字机)

通过结合使用 mdframed 和 fancyvrb,我可以使它运行得很好:

\DefineVerbatimEnvironment{termverb}{Verbatim}{commandchars=\\\{\}}
\newcommand{\shellcommand}[1]{\textbf{\$} {\fontfamily{cmtt}\fontshape{sl}\selectfont{#1}}}

\begin{document}

This is regular text.

\begin{mdframed}[backgroundcolor=black,fontcolor=white]
\begin{termverb}
\shellcommand{./do_something.py}
This is printed text that works properly.
\end{termverb}
\end{mdframed}

现在我想为此创建一个自定义环境。但是我无法让它工作。无论我使用哪种命令、环境和 verbatim 替代项的组合,它总是会导致解析错误。删除 mdframed 或 verbatim 有效,但同时删除它们则无效。这是我得到的最接近的结果,它产生了正确的输出,但在运行 pdflatex 时需要按 Enter 键约 10 次才能忽略错误:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\usepackage{mdframed}
\pagestyle{empty}

\DefineVerbatimEnvironment{termverb}{Verbatim}{commandchars=\\\{\}}
\newenvironment{terminal}
               {\begin{mdframed}[backgroundcolor=black,fontcolor=white]
               \begin{termverb}}
               {\end{termverb}
               \end{mdframed}}
\newcommand{\shellcommand}[1]{\textbf{\$} {\fontfamily{cmtt}\fontshape{sl}\selectfont{#1}}}

\begin{document}

This is regular text.

\begin{mdframed}[backgroundcolor=black,fontcolor=white]
\begin{termverb}
\shellcommand{./do_something.py}
This is printed text that works properly.
\end{termverb}
\end{mdframed}

Regular text in the middle.

\begin{terminal}
\shellcommand{./do_something.py}
This does not work!
\end{terminal}

Regular text at the end.

\end{document}

任何帮助都将不胜感激。谢谢。

编辑:事实证明,使用 fancyvrb 确实无法做到这一点。可行的解决方案是将逐字输入部分更改为 alltt。普通的逐字输入不起作用,因为我需要在 verb 环境中进行宏扩展。

答案1

使用简短的语法\command ... \endcommand进行定义。

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\usepackage{mdframed}
\pagestyle{empty}

\DefineVerbatimEnvironment{termverb}{Verbatim}{commandchars=\\\{\}}
\newenvironment{terminal}
  {\mdframed[backgroundcolor=black,fontcolor=white] \termverb}
  {\endtermverb\endmdframed}
\newcommand\shellcommand[1]{\textbf{\$} {\fontfamily{cmtt}\fontshape{sl}\selectfont{#1}}}

\begin{document}

    This is regular text.

\begin{mdframed}[backgroundcolor=black,fontcolor=white]
\begin{termverb}
\shellcommand{./do_something.py}
This is printed text that works properly.
\end{termverb}
\end{mdframed}

Regular text in the middle.

\begin{terminal}
\shellcommand{./do_something.py}
This does not work!
\end{terminal}

Regular text at the end.

\end{document}

在此处输入图片描述

答案2

mdframed有一个名为 的宏\surroundwithmdframed。请在使用 定义的环境中使用此宏\DefineVerbatimEnvironment。代码:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\usepackage{mdframed}
\pagestyle{empty}

\DefineVerbatimEnvironment{terminal}{Verbatim}{commandchars=\\\{\}}

\surroundwithmdframed[%
backgroundcolor=black,
fontcolor=white
]{terminal}

\newcommand{\shellcommand}[1]{%
  \textbf{\$} {\fontfamily{cmtt}\fontshape{sl}\selectfont #1}%
}

\begin{document}

This is regular text.

\begin{terminal}
\shellcommand{./do_something.py}
This is printed text that works properly.
\end{terminal}

Regular text in the middle.

\begin{terminal}
\shellcommand{./do_something.py}
This does not work!
\end{terminal}

Regular text at the end.

\end{document}

在此处输入图片描述

相关内容