GeneralImplementing grayed rounding box

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
csmgroup
Posts: 25
Joined: Sat Jul 17, 2010 6:58 pm

Implementing grayed rounding box

Post by csmgroup »

I'm trying to make a grayboxed environment/command as is attached.

I used tikz for round box, and minipage/verbatim to be used inside the graybox.

I came up with the following command, but it doesn't work. The verbatim inside the minipage doesn't compile.

Code: Select all

\newcommand{\graybox}[1]{%
\noindent\begin{tikzpicture}%
  \draw node[draw=black,fill=black!10,rounded corners,inner sep=2ex,text width=\mytikzwidth] {%
  #1
  };%
\end{tikzpicture}%
}%

%% ERROR - verbatim doesn't work. 
\graybox{%
\begin{minipage}[b]{\mytikzwidth}
...
\begin{verbatim}
java -cp \$CLOJUREJAR:\$CLASSPATH clojure.lang.Script \$1
\end{verbatim}
...
\end{minipage}
}
I also tried with the environment, which also doesn't work. The minipage isn't sit inside the minipage.

Code: Select all

\newenvironment{grayboxx}%
{\noindent\begin{tikzpicture}%
  \draw node[draw=black,fill=black!10,rounded corners,inner
  sep=2ex,text width=\mytikzwidth] {%
}%
{};%
\end{tikzpicture}%
}
Questions
  • * What's wrong with the LaTeX code? How can I use verbatim environment with minipage in the tikzpicture?
    * Could you teach me a better way to implement the grayboxed environment as is shown in the attached picture?
This is the whole code, and I attached the compiled result.

Code: Select all

\documentclass{article}

\usepackage{tikz}

\newlength{\mytikzwidth}
\setlength{\mytikzwidth}{\textwidth}
\addtolength{\mytikzwidth}{-4ex} % this 10ex is just rule of thumb.

\newcommand{\graybox}[1]{%
\noindent\begin{tikzpicture}%
  \draw node[draw=black,fill=black!10,rounded corners,inner sep=2ex,text width=\mytikzwidth] {%
  #1
  };%
\end{tikzpicture}%
}%

\newenvironment{grayboxx}%
{\noindent\begin{tikzpicture}%
  \draw node[draw=black,fill=black!10,rounded corners,inner
  sep=2ex,text width=\mytikzwidth] {%
}%
{};%
\end{tikzpicture}%
}

\begin{document}

%%%% CASE 1 - OK
\begin{minipage}[b]{\mytikzwidth}
clj command is as follows.

\begin{verbatim}
java -cp \$CLOJUREJAR:\$CLASSPATH clojure.lang.Script \$1
\end{verbatim}

hello.clj needs a namespace (ns) to let the modules know about this code.
\end{minipage}

%%%% CASE 2 - not working with verbatim
\graybox{%
\begin{minipage}[b]{\mytikzwidth}
clj command is as follows.

%\begin{verbatim}
java -cp \$CLOJUREJAR:\$CLASSPATH clojure.lang.Script \$1
%\end{verbatim}

hello.clj needs a namespace (ns) to let the modules know about this code.
\end{minipage}
}

%%%% CASE 3 - not inside the tikz
\begin{grayboxx}
\begin{minipage}[b]{\mytikzwidth}
clj command is as follows.

%\begin{verbatim}
java -cp \$CLOJUREJAR:\$CLASSPATH clojure.lang.Script \$1
%\end{verbatim}

hello.clj needs a namespace (ns) to let the modules know about this code.
\end{minipage}
\end{grayboxx}

\end{document}
Attachments
screenshot20100718at913.png
screenshot20100718at913.png (68.3 KiB) Viewed 4559 times
Screen shot 2010-08-02 at 1.48.22 PM.png
Screen shot 2010-08-02 at 1.48.22 PM.png (40.49 KiB) Viewed 4559 times
Last edited by csmgroup on Tue Aug 03, 2010 2:08 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Implementing grayed rounding box

Post by Juanjo »

It is a bit tricky to implement environments admitting verbatim code. Try the following example:

Code: Select all

\documentclass[a4paper]{article}

\usepackage{tikz}
\usepackage{lipsum}

\newlength{\RoundedBoxWidth}
\newsavebox{\GrayRoundedBox}
\newenvironment{GrayBox}[1][\dimexpr\textwidth-4.5ex]%
   {\setlength{\RoundedBoxWidth}{\dimexpr#1}
    \begin{lrbox}{\GrayRoundedBox}
       \begin{minipage}{\RoundedBoxWidth}}%
   {   \end{minipage}
    \end{lrbox}
    \begin{center}
    \begin{tikzpicture}%
       \draw node[draw=black,fill=black!10,rounded corners,%
             inner sep=2ex,text width=\RoundedBoxWidth]%
             {\usebox{\GrayRoundedBox}};
    \end{tikzpicture}
    \end{center}}

\begin{document}

\lipsum[1]

\begin{GrayBox}
  clj command is as follows.
  \begin{verbatim}
  java -cp \$CLOJUREJAR:\$CLASSPATH clojure.lang.Script \$1
  \end{verbatim}
  hello.clj needs a namespace (ns) to let the modules 
  know about this code.
\end{GrayBox}

\lipsum[2]

\begin{GrayBox}[0.75\textwidth]
  clj command is as follows.
  \begin{verbatim}
  java -cp \$CLOJUREJAR:\$CLASSPATH 
  clojure.lang.Script \$1
  \end{verbatim}
  hello.clj needs a namespace (ns) to let the modules 
  know about this code.
\end{GrayBox}

\lipsum[3]

\end{document}
At least for me, it works as expected. Note that the GrayBox environment has an optional argument to fix the width of the box (by default, \textwidth-4.5 ex). Modify the above code to fit your needs.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
csmgroup
Posts: 25
Joined: Sat Jul 17, 2010 6:58 pm

Re: Implementing grayed rounding box

Post by csmgroup »

Thanks a lot Juanjo, it works like a charm!
Post Reply