4

I have created a box using \newcommand, based on TikZ named "mybox". The equation easily insertable in it. But I want to insert a \lstlisting environment in it. But it doesn't work. How can I do it? Here is my code:

\documentclass{minimal}

\usepackage{times}

\usepackage{tikz}

\usepackage{listings}

\newcommand*\mybox[1]{\centering\tikz{\node [text width=15cm,rounded corners,fill=orange!50,inner sep=0.5ex,midway](char){#1};}}

\begin{document}

\mybox{

\begin{equation}

\int_S \mathbf{B}\cdot d\mathbf{s}

\end{equation}

}

\mybox{

\begin{lstlisting}[frame=single,language=PHP,caption=Php info\label{php_info}]
<?php
phpinfo();
?>

\end{lstlisting}

}

\end{document}
5

In this case, I would suggest you using the tcolorbox package to create the colored frame; the package offers the listings library and the tcblisting environment to let you use the features from the listings package:

\documentclass{article}
\usepackage{tikz}
\usepackage[listings]{tcolorbox}

\newcommand*\mybox[1]{\centering\tikz{\node [text width=15cm,rounded corners,fill=orange!50,inner sep=0.5ex,midway](char){#1};}}

\begin{document}

\mybox{%
\begin{equation}
\int_S \mathbf{B}\cdot d\mathbf{s}
\end{equation}%
}

\begin{tcblisting}{listing only,listing options={frame=single,language=PHP,caption=Php info,label={php_info}},
colback=orange!50,boxrule=0pt,width=\dimexpr15cm+1ex\relax}
<?php
phpinfo();
?>
\end{tcblisting}

\end{document}

enter image description here

If this construct is going to be used several times, it is better to use \newtcblisting to define an environment with the predefined options; something along these lines (the optional argument for the lisbox environment allows to pass additional options to the listings options)

\documentclass{article}
\usepackage{tikz}
\usepackage[listings]{tcolorbox}

\newtcblisting{lisbox}[1][]{%
  listing only,
  listing options={frame=single,language=PHP,#1},
  colback=orange!50,
  boxrule=0pt,
  width=15cm}

\begin{document}

\begin{lisbox}[caption=Php info,label={php_info}]
<?php
phpinfo();
?>
\end{lisbox}

\end{document}
  • Thank you very much for your quicky answer and facing me with tcblisting. – ferahfeza Jul 4 '13 at 17:41
  • 1
    @ferahfeza You're welcome! I updated my answer with a new code that might be useful. – Gonzalo Medina Jul 4 '13 at 17:56
  • @Medina, This is also nice and clean code. Thanks. – ferahfeza Jul 4 '13 at 18:11

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.