3

I have been trying to get this working for a while, but have been unable to find the appropriate solution which looks good. The idea is that I want to have a single line of three listings. I do this by having a figure which contains three minipages which each contain a listing.

A MWE is the following:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{listings}

\begin{document}


\begin{figure}
    \centering
\begin{minipage}[t]{.30\textwidth}
\begin{lstlisting}[caption={a}]
for (i in 1..n):
  . . .
  lock.lock()
  shared++
  lock.unlock()
  . . .
\end{lstlisting}
\end{minipage}
\begin{minipage}[t]{.30\textwidth}
\begin{lstlisting}[caption={b}]
for (i in 1..n):
  . . .
  lock.lock()
  shared++
  lock.unlock()
  . . .
\end{lstlisting}
\end{minipage}
\begin{minipage}[t]{.30\textwidth}
\begin{lstlisting}[caption={c}]
for (i in 1..n):
  . . .
  lock.lock()
  shared++
  lock.unlock()
  . . .
\end{lstlisting}
\end{minipage}
\end{figure}

\end{document}

enter image description here

This places all three listings on a single row. However, I would like each listing to be wider and the figure to stretch over the margins of the page.

Appreciate any help!

1

Put your minipages in a tabular and increase their width.

You'll have a warning Overfull \hbox but, in this case, is wanted.

To begin before the left margin put a \hspace*{-...} before your tabular.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{listings}
\usepackage{tabularx}
\usepackage{showframe}% only to show the page margins
\usepackage{mwe}% only to have some blind text
\begin{document}
\blindtext
\begin{figure}
\hspace*{-100pt}% to go to the left of the left margin 
\begin{tabular}{ccc}
\begin{minipage}[t]{.3\paperwidth}% increase the width according to your needs
\begin{lstlisting}[caption={a}]
for (i in 1..n):
  . . .
  lock.lock()
  shared++
  lock.unlock()
  . . .
\end{lstlisting}
\end{minipage} &
\begin{minipage}[t]{.3\paperwidth}
\begin{lstlisting}[caption={b}]
for (i in 1..n):
  . . .
  lock.lock()
  shared++
  lock.unlock()
  . . .
\end{lstlisting}
\end{minipage} &
\begin{minipage}[t]{.3\paperwidth}
\begin{lstlisting}[caption={c}]
for (i in 1..n):
  . . .
  lock.lock()
  shared++
  lock.unlock()
  . . .
\end{lstlisting}
\end{minipage}\\
\end{tabular}
\end{figure}
\end{document}

enter image description here

  • It seems that using "frame" in the lstlisting settings breaks this. That is for example, \begin{lstlisting}[frame=single, caption={a}]. :-( – Andreas Feb 28 '17 at 10:37
  • @Andreas It's better you ask a follow-up question, unfortunately, I'm not an expert... – CarLaTeX Feb 28 '17 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.