2

Given the following piece of code:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}


\DefineVerbatimEnvironment{shell}{Verbatim}{
  commandchars=\%\{\},
  label=\shelltitle,
  frame=single,
  samepage=true,
  formatcom=\setcounter{prompt}{0}\start
}
\newcommand{\shelltitle}{This is a shell}

\makeatletter
\def\start{\let\FV@FV@ProcessLine\FV@ProcessLine
  \def\FV@ProcessLine{\noindent\vrule height3ex depth2ex 
                      \hbox to\hsize{\kern\FV@FrameSep This is the shell prompt\hfil}%
                      \kern-.8pt\vrule\par
                      \let\FV@ProcessLine\FV@FV@ProcessLine
                      \FV@ProcessLine}%
}
\makeatother

\newcounter{prompt}
\newcommand{\prompt}{\stepcounter{prompt}\theprompt>}

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

The samepage=true option doesn't affect the heading line ("This is the shell prompt") and page break happens immediately after that line. How can I ensure the heading line resides on the same page of the rest of the listing?

1
  • 1
    Did you try adding \nobreak just after \kern-.8pt\vrule\par?
    – egreg
    Aug 2, 2012 at 13:59

1 Answer 1

3

As egreg mentioned in his comment, is enough to use \nobreak right after \kern-.8pt\vrule\par. In the following example, if you delete the \nobreak command and process the resulting code, you'll see the undesired effect mentioned (a page break right after the line "This is the shell prompt"); processing the document as it is (with \nobreak) you'll see that the page break inside the environment is suppressed and the whole text is moved to the second page:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}

\DefineVerbatimEnvironment{shell}{Verbatim}{
  commandchars=\%\{\},
  label=\shelltitle,
  frame=single,
  samepage=true,
  formatcom=\setcounter{prompt}{0}\start
}
\newcommand{\shelltitle}{This is a shell}

\makeatletter
\def\start{\let\FV@FV@ProcessLine\FV@ProcessLine
  \def\FV@ProcessLine{\noindent\vrule height3ex depth2ex 
                      \hbox to\hsize{\kern\FV@FrameSep This is the shell prompt\hfil}%
                      \kern-.8pt\vrule\par\nobreak
                      \let\FV@ProcessLine\FV@FV@ProcessLine
                      \FV@ProcessLine}%
}
\makeatother

\newcounter{prompt}
\newcommand{\prompt}{\stepcounter{prompt}\theprompt>}

\begin{document}
\vspace*{18cm}
\begin{shell}
%prompt echo foo{}
foo
%prompt echo bar
bar
\end{shell}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .