2

I am looking a way to wrap something with a frame. I try different approaches but finally choose to use fancybox package and minipage (because I need to control the width of the frame). It works pretty good for text-only content

\usepackage{fancybox}

\begin{document}
  \fbox{%
    \begin{minipage}{\textwidth}
      \textbf{Why we use negative angle in above calculate?}\newline
       Since .... we have to ....
    \end{minipage}
  }
\end{document}

However, if we add any environment like lstlisting or verbatim, it doesn't work

  \fbox{%
    \begin{minipage}{\textwidth}
      \textbf{Why we use negative angle in above calculate?}\newline
       Since .... we have to ....
       \begin{lstlisting}
          A = x+y;
          B = x^2-2y;
       \end{lstlisting}
    \end{minipage}
  }

Is that any way to make it work?

p.s. I have created a webpage for the user to type the latex stuff inside and whatever they type, I will like to wrap that with a framebox and generated a pdf with pdflatex. I cannot control what they type, they might type text, math, environment and/or mix all of those. So any better way to frame whatever the latex stuff inside?

3 Answers 3

3

This is because listings (including verbatim content) cannot be passed as arguments to other functions/macros (without care). Boxing the content first is okay though:

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\newsavebox{\codebox}% For saving code
\begin{document}
\begin{lrbox}{\codebox}
\begin{lstlisting}
A = x+y;
B = x^2-2y;
\end{lstlisting}
\end{lrbox}

\noindent
\fbox{%
  \begin{minipage}{\dimexpr\linewidth-2\fboxsep-2\fboxrule}
    \textbf{Why we use negative angle in above calculate?}\newline
      Since .... we have to .... \par
    \usebox{\codebox}
  \end{minipage}%
}
\end{document}

lrbox is an environment that takes one mandatory (box) argument which it will use to store the content in. I've made on called \codebox. Do this outside your \fbox and then use \usebox{\codebox} inside \fbox's argument.

Note the use of \noindent, occational % (see What is the use of percent signs (%) at the end of lines?) and a \dimexpr for the minipage size to avoid overfull \hbox warnings.


If you don't have control over what the user inserts and they could mix/match content, then you might be better off boxing everything and just \fbox-ing the output:

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\newsavebox{\userinput}% For saving user input
\begin{document}
\begin{lrbox}{\userinput}
\begin{minipage}{\dimexpr\linewidth-2\fboxsep-2\fboxrule}
  \textbf{Why we use negative angle in above calculate?}\newline
   Since .... we have to ....

\begin{lstlisting}
A = x+y;
B = x^2-2y;
\end{lstlisting}
\end{minipage}
\end{lrbox}

\noindent
\fbox{\usebox{\userinput}}%
\end{document}

Of course, mdframed has no problem with this kind of thing, and might be a better option; it also allows for frame breaking across the page boundary:

enter image description here

\documentclass{article}
\usepackage{listings,mdframed}% http://ctan.org/pkg/{listings,mdframed}
\begin{document}
\begin{mdframed}
  \textbf{Why we use negative angle in above calculate?}\newline
   Since .... we have to ....

\begin{lstlisting}
A = x+y;
B = x^2-2y;
\end{lstlisting}
\end{mdframed}

\end{document}

See the mdframed documentation for more details on making things fancy.

2
  • Hi Werner, thanks for the reply. I am wondering if this is the only way to make it work by using lrbox and with other stuffs like \par. I have a python script to generate the frame box, so whatever the user type, I just want to wrap it with a frame. But I cannot control what the user type, he may use listing or not or mix listing and other command. So any way to wrap whatever latex command, text, math and/or environments people typed? Thanks Feb 1, 2013 at 19:59
  • @user1285419: I've added two alternatives.
    – Werner
    Feb 1, 2013 at 20:06
2

The listings package provides in-built options for framing listings environments, either globally using (for example)

\lstset{frame=single}

or else locally using (for example)

\begin{lstlisting}[frame=single]

A complete MWE follows

screenshot

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}

\lstset{frame=single}

\begin{document}

\begin{lstlisting}[frame=single]
          A = x+y;
          B = x^2-2y;
       \end{lstlisting}

\begin{lstlisting}[backgroundcolor=\color{yellow!20}]
          A = x+y;
          B = x^2-2y;
\end{lstlisting}

\end{document}
0

Use and enviorment that accepted Verbatim content like adjustbox

\usepackage{adjustbox}
...

\begin{adjustbox}{minipage=[t][0.5\textheight]{1\textwidth},fbox}
  ...
  \begin{lstlisting}[language=..., ...]
    your code
  \end{lstlisting}
  ...
\end{adjustbox}

or you can leave void textheight for the auto height

\begin{adjustbox}{minipage=[t][]{1\textwidth},fbox}
1
  • 1
    While this might answer (the very old question ;-)) it's preferred to post complete examples
    – user31729
    Feb 15, 2015 at 9:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .