13

I want to include a long section of verbatim text, which is too long for a single page, inside a figure with a caption. Unfortunately, when the verbatim environment is put into a figure, it does not automatically split across pages. Here is a MWE that illustrates the problem which I am facing.

\documentclass[a4paper]{article}

\usepackage{showframe} % show diagram of page layout

\begin{document}
\begin{figure}[h!]
\begin{verbatim}
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./2014-01-30.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/size10.clo))
No file 2014-01-30.aux.
[1{/usr/local/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./2014-01-30.aux) )</usr/local/texlive/2013/texmf-dist/fonts/type1/public/amsf
onts/cm/cmr10.pfb>
Output written on 2014-01-30.pdf (1 page, 8170 bytes).
SyncTeX written on 2014-01-30.synctex.gz.
Transcript written on 2014-01-30.log.



This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./2014-01-30.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/size10.clo))
No file 2014-01-30.aux.
[1{/usr/local/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./2014-01-30.aux) )</usr/local/texlive/2013/texmf-dist/fonts/type1/public/amsf
onts/cm/cmr10.pfb>
Output written on 2014-01-30.pdf (1 page, 8170 bytes).
SyncTeX written on 2014-01-30.synctex.gz.
Transcript written on 2014-01-30.log.



This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./2014-01-30.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/size10.clo))
No file 2014-01-30.aux.
[1{/usr/local/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./2014-01-30.aux) )</usr/local/texlive/2013/texmf-dist/fonts/type1/public/amsf
onts/cm/cmr10.pfb>
Output written on 2014-01-30.pdf (1 page, 8170 bytes).
SyncTeX written on 2014-01-30.synctex.gz.
Transcript written on 2014-01-30.log.
\end{verbatim}
\caption{Three copies of output of \texttt{pdflatex}.}
\end{figure}
\end{document}

enter image description here

Werner
  • 603,163
  • 4
    You won't ever get a page break within a standard figure environment, but don't worry, there are other options :) Have a look at the listings package which can provide you with an environment that has captions- the documentation has lots of examples, including some that float – cmhughes Jan 31 '14 at 02:47

2 Answers2

12

Floating objects don't admit page breaks; you can use the listings package to typeset your verbatim text; you can use the caption key to provide a caption and (unless you declare the listing as a float), it will admit page breaks:

\documentclass[a4paper]{article}
\usepackage{showframe} % show diagram of page layout
\usepackage{listings}

\lstset{
  basicstyle=\small\ttfamily,
  breaklines=true,
  columns=fullflexible
}

\begin{document}

\begin{lstlisting}[caption={Three copies of output of \texttt{pdflatex}.}]
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./2014-01-30.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/size10.clo))
No file 2014-01-30.aux.
[1{/usr/local/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./2014-01-30.aux) )</usr/local/texlive/2013/texmf-dist/fonts/type1/public/amsf
onts/cm/cmr10.pfb>
Output written on 2014-01-30.pdf (1 page, 8170 bytes).
SyncTeX written on 2014-01-30.synctex.gz.
Transcript written on 2014-01-30.log.



This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./2014-01-30.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/size10.clo))
No file 2014-01-30.aux.
[1{/usr/local/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./2014-01-30.aux) )</usr/local/texlive/2013/texmf-dist/fonts/type1/public/amsf
onts/cm/cmr10.pfb>
Output written on 2014-01-30.pdf (1 page, 8170 bytes).
SyncTeX written on 2014-01-30.synctex.gz.
Transcript written on 2014-01-30.log.



This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./2014-01-30.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/size10.clo))
No file 2014-01-30.aux.
[1{/usr/local/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./2014-01-30.aux) )</usr/local/texlive/2013/texmf-dist/fonts/type1/public/amsf
onts/cm/cmr10.pfb>
Output written on 2014-01-30.pdf (1 page, 8170 bytes).
SyncTeX written on 2014-01-30.synctex.gz.
Transcript written on 2014-01-30.log.
\end{lstlisting}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
10

If you want to add a bit of 'bling' to your verbatim you can, for example, employ the tcolorbox package.

screenshot

In the code below, I have defined a new newtcblisting called cmhlisting which takes two mandatory arguments, and one optional:

  • [#1] is optional arguments that you wish to pass to the listings environment
  • {#2} is the caption
  • {#3} is the \label that can be used for \ref and friends

Here's the complete code- if you want it to float, then you need only uncomment the lines that detail float and floatplacement. The manual details lots and lots of other options- this is just something to get you started.

% arara: pdflatex
\documentclass[a4paper]{article}
\usepackage{showframe}
\usepackage{listings}
\usepackage{tcolorbox}
% tcolorbox libraries
\tcbuselibrary{breakable,skins,listings}

\makeatletter
\tcbset{
    addtolol/.style={list entry={\kvtcb@title},add to list={lol}{lstlisting}},
}
\makeatother

\newtcblisting[use counter=lstlisting]{cmhlisting}[3][]{%
    width=\textwidth,
    breakable,
    colback=blue!5!white,
    colframe=white!85!black,
    listing only,
    listing options={#1},
    center title,
    %float,
    %floatplacement=!ht,
    title={\color{black}{\scshape Listing \thetcbcounter}: ~#2},label={#3},
    addtolol,
}

\lstset{%
    basicstyle=\small\ttfamily,
    breaklines=true,
      columns=fullflexible,
}%
\begin{document}

reference: \ref{lst:yourlabelhere}

\begin{cmhlisting}{Three copies of output of \texttt{pdflatex}}{lst:yourlabelhere}
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./2014-01-30.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/size10.clo))
No file 2014-01-30.aux.
[1{/usr/local/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./2014-01-30.aux) )</usr/local/texlive/2013/texmf-dist/fonts/type1/public/amsf
onts/cm/cmr10.pfb>
Output written on 2014-01-30.pdf (1 page, 8170 bytes).
SyncTeX written on 2014-01-30.synctex.gz.
Transcript written on 2014-01-30.log.



This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./2014-01-30.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/size10.clo))
No file 2014-01-30.aux.
[1{/usr/local/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./2014-01-30.aux) )</usr/local/texlive/2013/texmf-dist/fonts/type1/public/amsf
onts/cm/cmr10.pfb>
Output written on 2014-01-30.pdf (1 page, 8170 bytes).
SyncTeX written on 2014-01-30.synctex.gz.
Transcript written on 2014-01-30.log.



This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./2014-01-30.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/size10.clo))
No file 2014-01-30.aux.
[1{/usr/local/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./2014-01-30.aux) )</usr/local/texlive/2013/texmf-dist/fonts/type1/public/amsf
onts/cm/cmr10.pfb>
Output written on 2014-01-30.pdf (1 page, 8170 bytes).
SyncTeX written on 2014-01-30.synctex.gz.
Transcript written on 2014-01-30.log.
\end{cmhlisting}
\end{document}
cmhughes
  • 100,947