如何使用 tiks 制作灰色圆框

发布于 2024-09-12 16:58:36 字数 2747 浏览 3 评论 0原文

这是此处提出的后续问题。我需要制作一个圆形灰色的盒子,如图所示。

根据答案,我尝试了一些东西,但效果不佳。那么,另一个问题来了。

我想出了以下命令,但它不起作用。迷你页内的逐字内容无法编译。

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

我也尝试过环境,也是不行。迷你页不在迷你页内。

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

问题

  • LaTeX 代码有什么问题?如何在 tikzpicture 中使用带有 minipage 的逐字环境?
  • 您能教我一种更好的方法来实现如附图所示的灰盒环境吗?

这是整个代码,我把编译结果附在了一起。

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

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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不语却知心 2024-09-19 16:58:36

我向 LaTeX 社区提出了同样的问题,我得到了以下是胡安乔的回答。

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

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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文