2

I have 2 side-by-side minipages and I would like to put a caption below them. Is there a way to do it?

\noindent\begin{minipage}{0.5\textwidth}

\begin{lstlisting}[mathescape=true]
P  -> if (x,y) in box(B,B,B,B) 
      then symbol(S,C) 
      else pebble
B  -> 0 | 1 | 2 | 3 | 4 | 5 | 6
S  -> ring(O,I,R,x,y)
O  -> chicken | pig
I  -> chicken | pig | pebble
R  -> 1 | 2 | 3
C  -> [red, green, blue][A2(A1)]
A1 -> x | y | x+y
A2 -> lambda z:0 | lambda z:1 | 
      lambda z:2 | lambda z:z%2 | 
      lambda z:z%2+1 | 
      lambda z:2*(z%2)
\end{lstlisting}

\end{minipage}%
\hfill
\begin{minipage}{0.6\textwidth}
  \includegraphics[width=0.7\textwidth]{assets/task_grid.png}
  \caption{HELLO}    
\end{minipage}

== I'd like a caption here ==
Mico
  • 506,678
Evan Pu
  • 203
  • 2
    \begin{figure} your code here \caption{I'd like a caption here} \end{figure} – Fran Jun 04 '20 at 06:25
  • @Fran that totally worked haha, thanks ! – Evan Pu Jun 05 '20 at 09:57
  • Thanks, but I did not pay attention that the code was a listing environment, so go with Mico's way (+1). Although this is completely off-topic, I only will add that with a few more settings, like here the environment could look a bit more like a code chunk, and less like something going oddly wrong in plain text. – Fran Jun 05 '20 at 10:27

1 Answers1

2

The lstlisting environment provides its own captioning mechanism.

In your example code, the total width of minipages is (at least) 110% of \textwidth; this cannot go well. In the code below, I use widths of 0.6 and 0.35 of \textwidth.

enter image description here

\documentclass{article}
\usepackage[demo]{graphicx} % remove 'demo' option in real document
\usepackage{listings}
\begin{document}

\begin{minipage}{0.6\textwidth}
\begin{lstlisting}[mathescape=true,caption=HELLO,captionpos=b]
P  -> if (x,y) in box(B,B,B,B) 
      then symbol(S,C) 
      else pebble
B  -> 0 | 1 | 2 | 3 | 4 | 5 | 6
S  -> ring(O,I,R,x,y)
O  -> chicken | pig
I  -> chicken | pig | pebble
R  -> 1 | 2 | 3
C  -> [red, green, blue][A2(A1)]
A1 -> x | y | x+y
A2 -> lambda z:0 | lambda z:1 | 
      lambda z:2 | lambda z:z%2 | 
      lambda z:z%2+1 | 
      lambda z:2*(z%2)
\end{lstlisting}
\end{minipage}%
\hfill
\begin{minipage}{0.35\textwidth}
\includegraphics[width=\textwidth]{assets/task_grid.png}
\end{minipage}

\end{document}
Mico
  • 506,678