1

I would like to place a listing next to a tikz picture so that the captions are vertically aligned on the same level. I found the \subcaptionbox command to be very helpful.

However, the listing's light gray background spreads up to the right-hand page end, also covering the tikzpicture. This won't happen if I include an image instead.

Here is an MWE:

\documentclass{scrartcl}

\usepackage{subcaption}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{tikz}

\begin{document} 
\begin{figure}
    \subcaptionbox{Code}[0.5\textwidth]
    {
        \input{testListing}
    }
    \subcaptionbox{Line}[0.5\textwidth]
    {
       \begin{tikzpicture}
            \draw   (1,0) to (4,0);
       \end{tikzpicture}
    }
    \caption{Source code and Line.}
\end{figure}
\end{document}

The inputfile testListing contains the following code (need to use ``\input` because having the code in the subcaptionbox block won't compile for whatever reason):

\begin{lstlisting}[backgroundcolor=\color{lightgray}]
Test
Test
\end{lstlisting}

The document looks like this:

enter image description here

How can I manage to have prevent the gray background to cover the tikz image?

  • 1
    Why not use two subfigure environments with [b] alignment option? – leandriis Mar 31 '18 at 10:43
  • Damn that's easy. In fact, I already have several of such cases in subfigures but when researching for a way to bottom align the captions, noone came up with this easy solution. I'll use that insetad of subcaptiobox. But still, I'd be interested in solving the described problem with subcaptionbox – Paddre Mar 31 '18 at 11:08
  • \lstset{linewidth=0.5\textwidth} just before \input can reduce the width of the gray background. Nevertheless, the figure starts with a huge indentation. – leandriis Mar 31 '18 at 11:32
2

It is just my opinion here, but I do not think it is useful to have two subcaptions in your case, since the caption is quite explicit by himself.

A different way to achieve something like this (without subcaptionbox) could be to use the tcolorbox package. It may not be what you need but you can have a look at the package documentation, as it offers a lot of integration possibilities regarding listings and tikzpictures

EDIT: improved box formatting

I went a bit further with this box, using a lot more options to look more academic. Thanks @samcarter for the awesome tikzducks package !

\documentclass{scrartcl}
\usepackage[listings,skins]{tcolorbox}
\usepackage{tikzducks}
\newtcblisting{mylisting}[1][]{%
listing only,% quite explicit
colback=white!90!black, % color of the background
colframe=white!90!black, % color of the frame
left=1pt,right=1pt,top=1pt,bottom=1pt,% margins
boxrule=0pt
}

\newtcolorbox[blend into=figures]{myfigure}[2][]{
skin=bicolor,% to get different colors for the two sides of the box
colback=white!90!black,% left background color
colbacklower=white,% right background color
sidebyside=true,% left-right blocks instead of upper-lower
attach boxed title to bottom center,% position of the title
fontupper=\ttfamily,% font used in the left block
title={#2},% title of the box
colbacktitle=white,% title background color
coltitle=black,% title font color
colframe=white,% title frame color
left=1pt,right=1pt,top=1pt,bottom=1pt,% margins
sharp corners,% shape of box corners
boxrule=0pt, % frame thickness
boxsep=1pt, % separation between box and content
#1% room for more options
}
\begin{document}
  \begin{myfigure}[label={fig-listing}]{Listing Box with tikzpicture}
      % Left block (Upper block) content
      \begin{mylisting}
          Here is a pretty duck
          Here is a pretty duck
          Here is a pretty duck
          Here is a pretty duck
          Here is a pretty duck
          Here is a pretty duck
          Here is a pretty duck
          Here is a pretty duck
      \end{mylisting}
      % Switch to Right block (Lower block) content
      \tcblower
      \centering
      \begin{tikzpicture}
          \duck[longhair=teal]
      \end{tikzpicture}

\end{myfigure}
Figure \ref{fig-listing} is a tcolorbox containing a listing and a tikzpicture. Thanks @samcarter.
\end{document}

enter image description here

EDIT n°2: Some more investigations about the OP's issue

I have tried to investigate a bit further the issue, and I think it is not at all related to the \subcaptionbox in itself but to the listings package. It seems to be due to the way listings modifies the behavior of latex during the build process, see this answer unfortunately lacking some explanations.

So one way around could be to get rid of the \subcaptionbox by using e.g. subfigure environments, which are basically minipage environments with some more captionning possibilities.

\documentclass{scrartcl}
\usepackage{subcaption}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{tikz}

\begin{document}
\begin{figure}
  \begin{subfigure}[b]{0.5\linewidth}
    \centering
     \begin{lstlisting}
      blablabla
    \end{lstlisting}
    \caption{Code}
  \end{subfigure}
  \begin{subfigure}[b]{0.5\linewidth}
    \centering
    \begin{tikzpicture}
      \draw (1,0) to (4,0);
    \end{tikzpicture}
    \caption{Line}
  \end{subfigure}

    \caption{Source code and Line.}
\end{figure}
\end{document}

enter image description here

  • Looks interesting, thanks for sharing. However, I'm writing an academic paper and I think such boxes are not considered academic style :-/ – Paddre Mar 31 '18 at 13:54
  • I see what you mean, I am in the same situation :), actually, you can modify the round corners, or other features using the options of the tcolorbox environment. – BambOo Mar 31 '18 at 15:04
  • I edited my answer with a bit more information on how to tweak the box, enjoy ! – BambOo Mar 31 '18 at 16:52
  • +1 for the duck...and for the edited answer, of course :-) Eventhough that works perfectly fine for me, I'd still be interested for how to solve it using subcaptionbox or an explanation of the behaviour with reasoning why it is not possible to achieve it using subcaptionbox. – Paddre Apr 1 '18 at 16:00
  • I will look into if I find some time :) – BambOo Apr 1 '18 at 16:02

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.