6

I am writing a technical report related to text processing and would like to use a figure to illustrate something like what is shown in figure 1 below.

How I think I would like my figure

So basically, it is just a figure with text. As I wan't to keep the "latex-look" in the entire document, I would prefer not doing it as I have done in this example (e.g. using notepad).

I am sorry for not providing a minimal working example, but I could not figure out what to search for other than e.g. "latex figures with text". This only results in descriptions on how to create figure captions.

So basically what I am asking for is guidance on how to do this, or what to search for.

Also, if someone knows if this is a proper way of illustrating what I am trying to do (like you see in the example figure) when it comes to academic reports, I would appreciate your input. I assume this would be preferable rather than just writing the lists of strings and integers (as in this example) out in raw text.

  • 4
    You can use what you want inside a float environment. Thus, you could use an array inside \begin{figure} <float content> \end{figure}. Using figure will make caption look like "Figure 1. xxxx" yet then, content is up to you. For several sub-figures, have a look at the subfig package. For displaying code, it's listing. It's then up to you to combine all these elements together (-; ! – ebosi Apr 11 '17 at 21:17
  • …or a minipage, inside which you could put whatever you want. – GuM Apr 11 '17 at 21:18
  • 1
    See my answer below that uses a slightly different approach. – ebosi Apr 11 '17 at 21:48
  • thanks for a really good answer @ebo ! I have been looking at it as you have made the Changes aswell. I am trying to do exactly what you suggest, but the background of my listings becomes grey. And it is not centered. But I will check the link you provided in the answer for the latter. Any thoughts on the grey background? – Stephen Johnson Apr 11 '17 at 21:50

1 Answers1

6

Following solution combines several "tricks":

First, all codes snippets are inside a figure environment. This enables you to use a caption. The later will say something : Figure 1. Caption content (Figure because the caption has been defined in a... figure environment!).

Then, we use subfigure environments to put several elements side-by-side. Using the package subcaption enables you to define a \caption inside each subfigure environment and thus add... a sub-caption.

Lastly, one use the listings package for displaying code within lstlisting environments. Beware: because code is printed alike verbatim, you should "reset" indentation inside lstlisting environments. Centering code snippets is not trivial. For that we make a detour, include lstlisting inside one-cell tabular, as explained in How to center a listing?.


enter image description here

\documentclass{article}
    \usepackage{subcaption}
    \usepackage{listings}
        \lstset{basicstyle=\ttfamily}
    \usepackage{lipsum}

\begin{document}
    \lipsum[1]
    \begin{figure}[htb]
        \centering
        \begin{subfigure}[t]{.5\linewidth}
            \centering
            \begin{tabular}{c}
% Don't indent the lstlisting environment!          
\begin{lstlisting}
[
  'foo',
  'bar',
  'baz',
]
\end{lstlisting}
             \end{tabular}
             \caption{Simply words}
        \end{subfigure}%
        \begin{subfigure}[t]{.5\linewidth}
            \centering
            \begin{tabular}{c}
\begin{lstlisting}
[
  3,
  2,
  1,
]
\end{lstlisting}
            \end{tabular}
            \caption{And numbers here}
        \end{subfigure}
        \caption{Very code. Much geek. Wow!}
    \end{figure}
    \lipsum[2]
\end{document}
ebosi
  • 11,692
  • thanks for a really good answer @ebo ! I have been looking at it as you have made the Changes aswell. I am trying to do exactly what you suggest, but the background of my listings becomes grey. And it is not centered. But I will check the link you provided in the answer for the latter. Any thoughts on the grey background? – Stephen Johnson Apr 11 '17 at 21:54
  • 1
    @StephenJohnson Have you tried compiling exactly the MWE in the question? If yes, do you still observe the same phenomenon? – ebosi Apr 11 '17 at 21:55
  • I am not sure exactly what MWE stands for, but I basically just copy pasted your suggestion and really made sure the indentations and Everything were the same. The difference is that I need to use this in a separate tex-file which is included by using \input{./file}.. so maybe the indentation in relation to the \begin{document} gets wrong because of this? – Stephen Johnson Apr 11 '17 at 21:58
  • I am so sorry. I just discovered that the template I am using had a \lstset{}with a backgroundcolor defined. Removing that parameter made Everything work perfectly. For some strange reason it is also centered now. Thanks alot for your help! – Stephen Johnson Apr 11 '17 at 22:02
  • 1
    My bad, MWE stands for minimal working example. Try copy-pasting the code above in a new file (make sure everything is the same) and compile it. Does code snippets appear centered? What are their background color? – ebosi Apr 11 '17 at 22:03
  • @StephenJohnson Don't worry. MWE are standalone code that helps tracking where does the error come from. Actually creating your own MWE (see link above) reproducing an artefact is a good way to debug your code! (For what regards code centering, compare options that were in your \lstset command with listings documentation. There might be an attribute regarding alignment.) – ebosi Apr 11 '17 at 22:06