LaTeX forum ⇒ GeneralImplementing grayed rounding box

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

Implementing grayed rounding box

Postby csmgroup » Mon Aug 02, 2010 9:02 pm

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.
  1. \newcommand{\graybox}[1]{%
  2. \noindent\begin{tikzpicture}%
  3. \draw node[draw=black,fill=black!10,rounded corners,inner sep=2ex,text width=\mytikzwidth] {%
  4. #1
  5. };%
  6. \end{tikzpicture}%
  7. }%
  8.  
  9. %% ERROR - verbatim doesn't work.
  10. \graybox{%
  11. \begin{minipage}[b]{\mytikzwidth}
  12. ...
  13. \begin{verbatim}
  14. java -cp \$CLOJUREJAR:\$CLASSPATH clojure.lang.Script \$1
  15. \end{verbatim}
  16. ...
  17. \end{minipage}
  18. }


I also tried with the environment, which also doesn't work. The minipage isn't sit inside the minipage.

  1. \newenvironment{grayboxx}%
  2. {\noindent\begin{tikzpicture}%
  3. \draw node[draw=black,fill=black!10,rounded corners,inner
  4. sep=2ex,text width=\mytikzwidth] {%
  5. }%
  6. {};%
  7. \end{tikzpicture}%
  8. }


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.
  1. \documentclass{article}
  2.  
  3. \usepackage{tikz}
  4.  
  5. \newlength{\mytikzwidth}
  6. \setlength{\mytikzwidth}{\textwidth}
  7. \addtolength{\mytikzwidth}{-4ex} % this 10ex is just rule of thumb.
  8.  
  9. \newcommand{\graybox}[1]{%
  10. \noindent\begin{tikzpicture}%
  11. \draw node[draw=black,fill=black!10,rounded corners,inner sep=2ex,text width=\mytikzwidth] {%
  12. #1
  13. };%
  14. \end{tikzpicture}%
  15. }%
  16.  
  17. \newenvironment{grayboxx}%
  18. {\noindent\begin{tikzpicture}%
  19. \draw node[draw=black,fill=black!10,rounded corners,inner
  20. sep=2ex,text width=\mytikzwidth] {%
  21. }%
  22. {};%
  23. \end{tikzpicture}%
  24. }
  25.  
  26. \begin{document}
  27.  
  28. %%%% CASE 1 - OK
  29. \begin{minipage}[b]{\mytikzwidth}
  30. clj command is as follows.
  31.  
  32. \begin{verbatim}
  33. java -cp \$CLOJUREJAR:\$CLASSPATH clojure.lang.Script \$1
  34. \end{verbatim}
  35.  
  36. hello.clj needs a namespace (ns) to let the modules know about this code.
  37. \end{minipage}
  38.  
  39. %%%% CASE 2 - not working with verbatim
  40. \graybox{%
  41. \begin{minipage}[b]{\mytikzwidth}
  42. clj command is as follows.
  43.  
  44. %\begin{verbatim}
  45. java -cp \$CLOJUREJAR:\$CLASSPATH clojure.lang.Script \$1
  46. %\end{verbatim}
  47.  
  48. hello.clj needs a namespace (ns) to let the modules know about this code.
  49. \end{minipage}
  50. }
  51.  
  52. %%%% CASE 3 - not inside the tikz
  53. \begin{grayboxx}
  54. \begin{minipage}[b]{\mytikzwidth}
  55. clj command is as follows.
  56.  
  57. %\begin{verbatim}
  58. java -cp \$CLOJUREJAR:\$CLASSPATH clojure.lang.Script \$1
  59. %\end{verbatim}
  60.  
  61. hello.clj needs a namespace (ns) to let the modules know about this code.
  62. \end{minipage}
  63. \end{grayboxx}
  64.  
  65. \end{document}
Attachments
screenshot20100718at913.png
screenshot20100718at913.png (68.3 KiB) Viewed 1206 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 1206 times
Last edited by csmgroup on Tue Aug 03, 2010 2:08 am, edited 1 time in total.

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Juanjo
Posts: 656
Joined: Sat Jan 27, 2007 12:46 am

Postby Juanjo » Mon Aug 02, 2010 11:12 pm

It is a bit tricky to implement environments admitting verbatim code. Try the following example:
  1. \documentclass[a4paper]{article}
  2.  
  3. \usepackage{tikz}
  4. \usepackage{lipsum}
  5.  
  6. \newlength{\RoundedBoxWidth}
  7. \newsavebox{\GrayRoundedBox}
  8. \newenvironment{GrayBox}[1][\dimexpr\textwidth-4.5ex]%
  9. {\setlength{\RoundedBoxWidth}{\dimexpr#1}
  10. \begin{lrbox}{\GrayRoundedBox}
  11. \begin{minipage}{\RoundedBoxWidth}}%
  12. { \end{minipage}
  13. \end{lrbox}
  14. \begin{center}
  15. \begin{tikzpicture}%
  16. \draw node[draw=black,fill=black!10,rounded corners,%
  17. inner sep=2ex,text width=\RoundedBoxWidth]%
  18. {\usebox{\GrayRoundedBox}};
  19. \end{tikzpicture}
  20. \end{center}}
  21.  
  22. \begin{document}
  23.  
  24. \lipsum[1]
  25.  
  26. \begin{GrayBox}
  27. clj command is as follows.
  28. \begin{verbatim}
  29. java -cp \$CLOJUREJAR:\$CLASSPATH clojure.lang.Script \$1
  30. \end{verbatim}
  31. hello.clj needs a namespace (ns) to let the modules
  32. know about this code.
  33. \end{GrayBox}
  34. \lipsum[2]
  35. \begin{GrayBox}[0.75\textwidth]
  36. clj command is as follows.
  37. \begin{verbatim}
  38. java -cp \$CLOJUREJAR:\$CLASSPATH
  39. clojure.lang.Script \$1
  40. \end{verbatim}
  41. hello.clj needs a namespace (ns) to let the modules
  42. know about this code.
  43. \end{GrayBox}
  44.  
  45. \lipsum[3]
  46.  
  47. \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.

Link:
BBcode:
HTML:
Hide post links
Show post links

csmgroup
Posts: 25
Joined: Sat Jul 17, 2010 6:58 pm
Location: Austin, Texas, USA

Postby csmgroup » Tue Aug 03, 2010 2:08 am

Thanks a lot Juanjo, it works like a charm!

Link:
BBcode:
HTML:
Hide post links
Show post links


Return to “General”

Who is online

Users browsing this forum: No registered users and 10 guests