1

I defined the following command \recomm to print a box with a separated body and a title:

\newcommand{\recomm}[3]{
  \vspace{0.5cm}
  \noindent 
  \begin{tikzpicture}
    \node [rectangle, inner sep=10pt] (box){%
        \begin{minipage}{\dimexpr\linewidth-20pt-\pgflinewidth\relax}
            #2
        \end{minipage}
    };
    \node [rectangle, inner sep=10pt, fill=black!15, below right] (box2) at (box.south west){%
        \begin{minipage}{\dimexpr\linewidth-20pt-\pgflinewidth\relax}
            #3
        \end{minipage}
    };
    \draw[draw=blue!75!black, very thick] 
      -- (box2.south west)
      -- (box.north west)
      -- (box.north east)
      -- (box2.south east)
      -- (box2.south west);
    \node[above right, fill=blue!75!black, text=white, font=\bfseries] at (box.north west) {#1};
  \end{tikzpicture}%
}

This works fine and prints a nice box when used with simple input:

\recomm{Title}{%
  Some content in the first part
}{%
  Some content in the second part
}

Working box

However, when I try to insert a code listing using the lstlisting environment, I'm getting all sorts of errors:

\recomm{Title}{%
  Some content in the first part
}{%
  Some content in the second part

  \begin{lstlisting}
    a=b
  \end{lstlisting}
}
Argument of \lst@next has an extra }.
<inserted text> 
                \par 
l.185 }

I tried using \newenvironment, but that allows only for a single environment where I need two (the first and second part of the picture). What is the recommended way to solve this nicely?

(I'd like to avoid tcolorbox, as I have to work with a very incomplete texlive environment where that package and many of its dependencies are not included.)


Minimal not working example:

\documentclass{article}

\usepackage{listings}
\usepackage{tikz}

\newcommand{\recomm}[2]{
  \begin{tikzpicture}
    \node [rectangle, inner sep=10pt] (box){%
        \begin{minipage}{\dimexpr\linewidth-20pt-\pgflinewidth\relax}
            #1
        \end{minipage}
    };
    \node [rectangle, inner sep=10pt, fill=black!15, below right] (box2) at (box.south west){%
        \begin{minipage}{\dimexpr\linewidth-20pt-\pgflinewidth\relax}
            #2
        \end{minipage}
    };
}

\begin{document}

\recomm{%
  Some content in the first part
}{%
  Some content in the second part

  \begin{lstlisting}
    a=b
  \end{lstlisting}
}

\end{document}
janoliver
  • 377
  • 2
    Listings and other types of verbatim link constructions cannot generally be used in arguments for macros. Make an env based interface instead and that will probably work. Also have a look at the tcolorbox package which probably include what you are looking for. – daleif Dec 03 '19 at 11:07
  • 1
    Additionally next time please incldue a full minimal self contained example instead of just a sniplet. Users are much less likely to help if thye have to guess part of a document in order to test your code. – daleif Dec 03 '19 at 11:08
  • In addition to what @daleif is saying, tcolorbox allows you to use all TikZ commands for your boxes using overlay. (The depicted box does not need that, it is very easy to reproduce it with tcolorbox without overlay, I think.) –  Dec 03 '19 at 16:07
  • Thank you for your answers. I'll provide a minimal (not-)working example in a minute. As stated in my question, I can't use tcolorbox. – janoliver Dec 03 '19 at 16:16
  • 1
    You may want to look at https://tex.stackexchange.com/q/29971 to see how much effort it will be to accomplish what you want. tcolorbox does a lot of things behind the scenes, you'd basically have to redo them. Personally I would think it would be easier to fix the dependencies on your installation. –  Dec 03 '19 at 17:14

0 Answers0