Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the grayed-round box using tiks

Tags:

latex

tikz

This is a follow-up question asked here. I need to make a round-grayed box as is shown.

Based on the answers, I tried something, but not so fruitful. So, another questions coming.

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

\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.

\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 with the compiled result.

\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}
like image 411
prosseek Avatar asked Aug 02 '10 19:08

prosseek


People also ask

How do you draw a circle in TikZ?

The point is actually a circle drawn by \filldraw[black] , this command will not only draw the circle but fill it using black. In this command the centre point (0,0) and the radius (2pt) are declared.

How do you draw a square in TikZ?

A rectangle shape can be drawn by using the option rectangle to the \node command. Following line creates a rectangle node named (r) at coordinate (0,0). The rectangle node shape is always defined and no TikZ library is needed to be loaded (The same with circle node shape).

How do you draw a thick line in TikZ?

Line thickness You can use: ultra thin, very thin, thin, semithick, thick, very thick and ultra thick. You can specify your custom width using the line width option of the \draw command. The default unit is pt.


1 Answers

I asked the same question to LaTeX Community, and I got the following answer from Juanjo.

\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}
like image 172
prosseek Avatar answered Sep 18 '22 08:09

prosseek