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.

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?

share|improve this question
1  
Did you try adding \nobreak just after \kern-.8pt\vrule\par? –  egreg Aug 2 '12 at 13:59
add comment

1 Answer

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}
share|improve this answer
add comment

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.