8

I am writing a set of tutorial documents on some command-line based tools. This tutorials need to show a "screen session". Something along the lines of:

$ git status
# on branch "master"

I want to be able to:

  1. mark the shell prompt with a particular color (I always use the initial $ to indicate I'm on the prompt)
  2. Identify the command output with a different color
  3. Identify keywords in the shell commands

I was looking at the listings package and it seems to be on the right path, but I couldn't find a way to produce that kind of output. I saw the documentation on how to produce a different "language" for the listings package, but I couldn't get myself around that, since I couldn't find much documentation.

Is there a public implementation of such "language" specification? If not, can you give me some tips on how to define it?

  • So far, I came up with the following, which is not yet complete, since it won't inherit the settings from the shell language... \lstdefinelanguage{screensession}{ basicstyle=\color{black}\ttfamily\small, delim=**[il][\color{lightgray}\$\color{blue}]{\$} } – ruoso May 18 '11 at 19:55
2

This might answer some of your questions. For the prompt, you may use the literate option. Two ways are shown to color output. The first way is not very good but it gets the job done for very short and simple output. The other way is to define an output environment. The result from the two are different : when using the escape character, you are leaving the listings mode and the spacing between the letters is not the same. See the output text in red below.

\documentclass{minimal}

\usepackage{listings}
\usepackage{xcolor}

\lstnewenvironment{code}
    {\lstset{%
        basicstyle=\ttfamily,
        escapechar=~,
        morekeywords={foo,bar},
        keywordstyle=\color{green},
        literate={\$}{{\textcolor{blue}{\$}}}1}
    }
    {}

\lstnewenvironment{codeoutput}
    {\lstset{basicstyle=\ttfamily\color{red}}}
    {}

\begin{document}

\begin{code}
$ git status
# on branch "master"
foo in bar is blah

for short output
~\color{red}This is output\color{black}~
\end{code}

\begin{codeoutput}
This is output
\end{codeoutput}

\end{document}

The output is

output

|improve this answer|||||
2

I'll post my setup that I originally made for ActionScript:

\lstdefinelanguage{ActionScript} { %this is the name that you are going to use when you want to use the formatting
    basicstyle=\ttfamily\scriptsize, %font family & size
    sensitive=true, %if you want your keywords to be case sensitive
    morestring=[b]",   
    %morestring: if single quotes open and close string or character literals in 
    %the same way as double quotes do in the example.
    morecomment=[l][\color{flashgreen}\ttfamily]{//}, 
    %morecomment has to do with comments in the text, read about them in the documentation
    morecomment=[s][\color{flashgreen}\ttfamily]{/*}{*/},
    morecomment=[s][\color{flashgreen}\ttfamily]{/**}{*/},
    stringstyle=\color{flashred}, %string style (red in this case)
    commentstyle=\color{flashgreen}, %comment style
    showstringspaces=false, %the following are well documented in the listings docs
    numberstyle=\scriptsize, %you can include almost any standard settings that you can efine in 
    numberblanklines=true,
    showspaces=false,
    showtabs=false,        
    emph =
    {[1]
            here, you, define, your, keywords, separated, by, commas, even, the, last,
            one,
    },
    emphstyle={[1]\color{flashblue}\textbf}, %set the style of these keywords
    emph = 
    {[2]
        %more keywords with different style
    },
    emphstyle={[2]\color{flashlblue}\textbf}
}

Just ask if anything is unclear and I'll try to help you as much as I can.

|improve this answer|||||
  • I'm sorry... have you read my question? If I wanted just the bash language, then I'll be using it... I wanted a way to define something that embed shell, but also includes the $PS1 marker as well as a different format for command output. – ruoso May 18 '11 at 19:54
  • 1
    @ruoso: I did indeed read your question. I seem to be misunderstanding a lot tonight. What I could understand, you wanted to know if anyone knew about an already formatted listings language for the language that you want to highlight and if not you wanted pointers on how to define a language in listings. If I totally misunderstood your question and wasted your time, then I am truly sorry and please accept my apologies. – benregn May 18 '11 at 20:13
1

I can recommend the minted package which uses pygments.

\documentclass{article}
\usepackage{minted}
\begin{document}
\begin{minted}{bash}
$ git status
# on branch "master"
foo in bar is blah

for short output
$ for i in `seq 2`; do 
for> echo "Test \"number\" ${i}  {}";
done
Test "number" 1  {}
Test "number" 2  {}
\end{minted}
\end{document}

enter image description here

|improve this answer|||||
  • If you encounter ANSI colors in the output of a program then you can use ansi2html which I extended to convert to LaTeX including the colors. – ypid May 20 '14 at 11:32

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.