LaTeX Books

Cookbook

LaTeX Beginners Guide

Limited discount 50%
coupon code tDRet6Y

click here to read the whole story

LaTeX users online

In total there are 11 users online :: 1 registered, 0 hidden and 10 guests (based on users active over the past 5 minutes)
Most users ever online was 1327 on Tue Nov 05, 2013 7:11 pm

Users browsing this forum: Exabot [Bot] and 10 guests

Implementing grayed rounding box

LaTeX Forum: General

Add tags LaTeX specific issues not fitting into one of the other forums of this category.

Postby csmgroup on Mon Aug 2nd, 2010

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  •  Open in writeLaTeX
\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  •  Open in writeLaTeX
\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  •  Open in writeLaTeX
\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 1018 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 1018 times
Last edited by csmgroup on Tue Aug 3rd, 2010, edited 1 time in total.
csmgroup
 
Posts: 25
Joined: Sat Jul 17th, 2010
Location: Austin, Texas, USA

Postby Juanjo on Mon Aug 2nd, 2010

It is a bit tricky to implement environments admitting verbatim code. Try the following example:
Code: Select all  •  Open in writeLaTeX
\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.
User avatar
Juanjo
 
Posts: 656
Joined: Sat Jan 27th, 2007

Postby csmgroup on Tue Aug 3rd, 2010

Thanks a lot Juanjo, it works like a charm!
csmgroup
 
Posts: 25
Joined: Sat Jul 17th, 2010
Location: Austin, Texas, USA


Return to General

LaTeX users online

Users browsing this forum: Exabot [Bot] and 10 guests