2

I would like to format terminal output as follows:

  • white text on black background
  • commands written by the user are emphasized (slanted typewriter)

Using a combination of mdframed and fancyvrb I got this to work quite nicely:

\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}

Now I'd like to create a custom environment for this. However I can't get that to work. No matter which combination of commands, environments and verbatim alternates I use, it will always lead to a parse error. Removing mdframed or verbatim works, but both of them at the same time do not. This is the closest I got, it produces the correct output but requires pressing enter about 10 times when running pdflatex to ignore the errors:

\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}

Any help would be appreciated. Thanks.

Edit: It turns out this is not really possible with fancyvrb. The working solution was to change to alltt for the verbatim input part. Plain verbatim does not work because I need to get macro expansion inside the verb environment.

  • 1
    Drop mdframed and use tcolorbox instead. It has loads of options to frame code. – Ulrike Fischer Nov 20 '18 at 12:51
  • Can you apply the solution provided in this answer? – Arash Esbati Nov 20 '18 at 18:30
1

Use the short syntax \command ... \endcommand for the definition.

\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}

enter image description here

0

mdframed has a macro called \surroundwithmdframed. Use this macro on your environment defined with \DefineVerbatimEnvironment. Code:

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.