Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

put listing in tikzpicture

Tags:

latex

tex

tikz

I use the following code to create a box for equations. It uses Tikz to create the box.

\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{amsmath,amssymb}%      pour les maths
\usepackage{enumitem}
\usepackage{varwidth}
\usepackage{listings}
\usetikzlibrary{calc}

\newcommand{\mybox}[4][\textwidth-\pgfkeysvalueof{/pgf/inner xsep}-2mm]{%
\begin{figure}[!h]
\centering
\begin{tikzpicture}
\node[line width=.5mm, rounded corners, draw=#2, inner ysep=10pt, text width=#1, outer sep=0] (one) {\vspace*{15pt}\\\begin{varwidth}{\textwidth}#4\end{varwidth}};
\node[text=white,anchor=north east,align=center, minimum height=20pt] (two) at (one.north east) {#3 \hspace*{.5mm}};
\path[fill=#2] 
    (one.north west|-two.west) --
    ($(two.west)+(-1.5cm,0)$) 
    to[out=0,in=180] (two.south west) --
    (two.south east) [rounded corners] --
    (one.north east) -- 
    (one.north west) [sharp corners] -- cycle;
\node[text=white,anchor=north east,align=center, minimum height=20pt, text height=2ex] (three) at (one.north east) {#3 \hspace*{.5mm}};
\end{tikzpicture} 
\end{figure}
}

\begin{document}
\mybox{green!70!black}{The Caption}{
\begin{enumerate}
\item Show that\\
$\displaystyle D_2f(x,y) = \frac{\partial {}}{\partial{y}} \biggl( \int_0^xg_1 (t,0) \ dt + \int_0^y g_2(x,s) \ ds \biggr)$
\item prove that\\
$\displaystyle \biggl(\forall x\in\mathbb{R}  \biggr)\biggl(\forall y \in \mathbb{R} \biggr) x\neq y\, \text{ and } \, x+y \neq 2 \implies x^{2}-2x \neq y^2-2y$ 
\end{enumerate}

}
\end{document}

but Now I want to put listing inside the box When I put the listing inside the mbox I got an error

\begin{lstlisting}[language=R]
fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) {
[omitted statements]
if (graph)
par(pch="*", ...)
[more omissions]
}
\end{lstlisting}

the error is

! Argument of \lst@next has an extra }.
like image 488
user3073682 Avatar asked Apr 02 '16 11:04

user3073682


1 Answers

You cannot use listings inside arguments directly. You either need to escape and properly prepare the verbatim code or store it somewhere else. See section 6.1 of the listing manual for more information.

Store the listing in a box

\begin{document}
\begin{lrbox}{\mylisting}
\begin{lstlisting}[language=R]
fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) {
[omitted statements]
if (graph)
par(pch="*", ...)
[more omissions]
}
\end{lstlisting}
\end{lrbox}

\mybox{green!70!black}{The Caption}{
  \usebox\mylisting
}
\end{document}

Store the listing in an external file

\lstinputlisting{external_file.R}

Add line feeds ^^J

\begin{lstlisting}[language=R]^^J
fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) {^^J
[omitted statements]^^J
if (graph)^^J
par(pch="*", ...)^^J
[more omissions]^^J
}^^J
\end{lstlisting}

enter image description here

Note: You will probably need to resize your box for that long first line of code.

like image 69
pchaigno Avatar answered Nov 04 '22 05:11

pchaigno