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 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

share|improve this question
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 at 2:47
add comment

2 Answers

up vote 12 down vote accepted

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

share|improve this answer
add comment

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}
share|improve this answer
    
This is interesting, and it looks great. Thanks! –  I Like to Code Jan 31 at 13:55
    
I just tried to compile your code, but I get an error ! Missing number, treated as zero. <to be read again> [ l.15 \newtcblisting[u Do you have any idea why I might be getting this error? –  I Like to Code Feb 3 at 22:16
    
@ILiketoCode works fine for me (as is). Try copying and pasting it into a new file- sounds like you have modified it –  cmhughes Feb 3 at 23:38
    
I tried to copy and paste into a new file, and it doesn't work. Strange! –  I Like to Code Feb 4 at 1:37
1  
@ILiketoCode I guess it's a version problem because the code works fine for me. You should check if tcolorbox version v2.61 (2014/01/10) or v2.70 (2014/02/06) is installed. I guess v2.51 (2013/09/16) could also be new enough (I have not tested this). –  Thomas F. Sturm Feb 6 at 14:06
show 2 more comments

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.