Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to define a new environment, which emulates a shell. The way I would like to use it:

\begin{shell}
\item{echo foo}{foo}
\item{echo bar}{bar}
\end{shell}

Which should produce:

This is my shell prompt
1> echo foo
foo
2> echo bar
bar

What I have so far:

\newcommand{shellprompt}{This is my shell prompt}
\newenvironment{shell}{
  \shellprompt
  \begin{enumerate}[\hspace{15px} 1\textgreater]
    \let\olditem\item
    \renewcommand\item{\olditem}
  }{
  \end{enumerate}
}

How do I redefine the \item command so it accepts a second parameter and prints it on a second line?

Bonus: How do I wrap everything (prompt + items) in a verbatim environment?

share|improve this question
    
Why \item? You can define your own custom macro with any number of arguments, instead of redefining a macro already used by latex for other purposes. –  JLDiaz Jul 23 '12 at 12:14

1 Answer 1

up vote 6 down vote accepted

This is a possibility:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{shell}{Verbatim}{commandchars=\%\{\},formatcom=\setcounter{prompt}{0}}
\newcounter{prompt}
\newcommand{\prompt}{\stepcounter{prompt}\theprompt>}

\begin{document}
\begin{shell}
%prompt echo foo
foo
%prompt echo bar
bar
\end{shell}
\end{document}

In the shell command you'll need to escape %, { and } with a %, so, to print

% { }

you have to input

%% %{ %}

enter image description here

If you want to add the same first line to all environments,

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{shell}{Verbatim}{commandchars=\%\{\},formatcom=\setcounter{prompt}{0}\start}
\newcounter{prompt}
\newcommand{\prompt}{\stepcounter{prompt}\theprompt>}
\newcommand{\start}{\noindent This is my shell prompt\par}

\begin{document}
\begin{shell}
%prompt echo foo{}
foo
%prompt echo bar
bar
\end{shell}
\end{document}

Just redefine \start to suit.

share|improve this answer
    
Right. But what about the prompt used as "header" (the "This is my shell prompt" line shown only once for each shell environment)? –  Roberto Aloi Jul 23 '12 at 13:20
    
Awesome. I've also realized that the fancyvrb package supports labels, which for me represents a valid alternative for the "header". Thanks so much. –  Roberto Aloi Jul 23 '12 at 13:59
    
The start command trick doesn't work well when a frame is used in the environment. –  Roberto Aloi Jul 24 '12 at 10:08
    
@RobertoAloi No, it doesn't. :( –  egreg Jul 24 '12 at 10:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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