Latex two captioned verbatim environments side-by-side
Asked Answered
I

3

8

How to get two verbatim environments inside floats with automatic captioning side-by-side?

\usepackage{float,fancyvrb}
...
\DefineVerbatimEnvironment{filecontents}{Verbatim}%
    {fontsize=\small,
        fontfamily=tt,
        gobble=4,
        frame=single,
        framesep=5mm,
        baselinestretch=0.8,
        labelposition=topline,
        samepage=true}

\newfloat{fileformat}{thp}{lof}[chapter]
\floatname{fileformat}{File Format}

\begin{fileformat}
    \begin{filecontents}
    A  B  C
    \end{filecontents}
    \caption{example.abc}
\end{fileformat}

\begin{fileformat}
    \begin{filecontents}
    C  B  A
    \end{filecontents}
    \caption{example.cba}
\end{fileformat}

So basically I just need those examples to be side-by-side (and keeping automatic nunbering of caption). I've been trying for a while now.

Insurrectionary answered 6/6, 2010 at 10:22 Comment(0)
I
3

Found the soulution finally.

\usepackage{caption}

\begin{fileformat}[h]
  \centering
  \begin{minipage}[b]{0.4\textwidth}
    \begin{filecontents}
    A B C
    \end{filecontents}
    \captionof{fileformat}{example.abc}
  \end{minipage}
  \quad
  \begin{minipage}[b]{0.4\textwidth}
    \begin{filecontents}
    C B A
    \end{filecontents}
  \captionof{fileformat}{example.cba}
  \end{minipage}
\end{fileformat}

The problem solution is to make a caption independently from environment using caption package macro \captionof{fileformat}{Our Caption}.

Insurrectionary answered 6/6, 2010 at 17:31 Comment(0)
R
4

For captioning verbatim environments you can either use listings (which will offer much more than just plain captioning, syntax highlighting and line numbering come for free too) or define your own float environment using the package with the same name.

An example (from WikiBooks):

\documentclass{article}

\usepackage{float}

\floatstyle{ruled}
\newfloat{program}{thp}{lop}
\floatname{program}{Program}

\begin{document}

\begin{program}
  \begin{verbatim}

class HelloWorldApp {
  public static void main(String[] args) {
    //Display the string
    System.out.println("Hello World!");
  }
}
\end{verbatim}
  \caption{The Hello World! program in Java.}
\end{program}

\end{document}
Rub answered 6/6, 2010 at 12:54 Comment(1)
I'm already using listing package for formatting algorithms, but I'm not aware that there is a possibility to define multiple automatic captioning. (Like different counting for "Algorithm" and "Format"). Also I wouldn't still know how to put those two verbatim environments side-by-side.Insurrectionary
I
3

Found the soulution finally.

\usepackage{caption}

\begin{fileformat}[h]
  \centering
  \begin{minipage}[b]{0.4\textwidth}
    \begin{filecontents}
    A B C
    \end{filecontents}
    \captionof{fileformat}{example.abc}
  \end{minipage}
  \quad
  \begin{minipage}[b]{0.4\textwidth}
    \begin{filecontents}
    C B A
    \end{filecontents}
  \captionof{fileformat}{example.cba}
  \end{minipage}
\end{fileformat}

The problem solution is to make a caption independently from environment using caption package macro \captionof{fileformat}{Our Caption}.

Insurrectionary answered 6/6, 2010 at 17:31 Comment(0)
D
2

Use minipage like in this example, which places two pictures side by within a figure float with separate captions

\begin{figure}[htbp]
  \centering
  \begin{minipage}[b]{5 cm}
    \includegraphics{filename 1} 
    \caption{caption 1}
    \label{labelname 1}
  \end{minipage}
  \begin{minipage}[b]{5 cm}
    \includegraphics{filename 2}  
    \caption{caption 2}
    \label{labelname 2}
  \end{minipage}
\end{figure}
Decarbonate answered 6/6, 2010 at 10:41 Comment(6)
Sure but how do I get the correct automatic captioning? Figure does it correctly but displays wrong prefix for captions. If I use fileformat instead of figure - it displays only one of the captions.Insurrectionary
Ah, I think that's the float package defining it's own caption placement styles. Have you tried the different \floatstyle{} options?Decarbonate
Yes, tried - ruled, plain, boxed. It's seems that if I'm not using figure it doesn't recognize those as being two separate things.Insurrectionary
Are you using figures at all? Would it be easier to modify the Figure environment than to try and get this working with the float package?Decarbonate
Yes I'm using figures, two custom floats and lstlisting.Insurrectionary
OK, a very kludgy way of doing what you want would be to use the float packages H option which makes it so that you can place the float exactly where you want it. Use the multicol package and switch to \twocolums, place two fileformat floats with the H option, and then switch back to \onecolumn.Decarbonate

© 2022 - 2024 — McMap. All rights reserved.