![如何突出显示包含 tikzpicture 的段落](https://linux22.com/image/282042/%E5%A6%82%E4%BD%95%E7%AA%81%E5%87%BA%E6%98%BE%E7%A4%BA%E5%8C%85%E5%90%AB%20tikzpicture%20%E7%9A%84%E6%AE%B5%E8%90%BD.png)
我正在尝试突出显示包含使用 tikz 绘制的图形的整个段落。我想突出显示文本以及图形周围的空白区域,而图形本身应具有通常的白色背景(我的示例中的矩形仅表示内部不应有阴影)。
\documentclass{article}
\usepackage{tikz}
\begin{document}
Some text of the main body of the document.
\paragraph{Paragraph}
Some text of the paragraph.
\begin{figure}[h]
\centering
\begin{tikzpicture}
\node at (0,0) [rectangle,draw] {
\begin{tikzpicture}
\draw [->] (-.5,0) -- (5,0) node [below] {$x$};
\draw [->] (0,-.5) -- (0,3) node [left] {$y$};
\end{tikzpicture}};
\end{tikzpicture}
\caption{The rectangle only delimits the area without shaded background; it needn't be rendered in actual paragraph.}
\label{fig:label}
\end{figure}
Some text of the paragraph. \\
Some text of the main body of the document.
\end{document}
我在用着这个话题作为参考。
这有可能做到吗?
答案1
举个例子mdframed
。你需要使用包H
中的浮点说明float
符来figure
避免它浮动。
我已将你置于\paragraph
一个新定义的colorpar
环境中,并且还为 提供了白色背景tikzpicture
。
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usepackage[framemethod=TikZ]{mdframed}
\newmdenv[%
innertopmargin=-\topskip,
skipabove=.5\topskip,
leftmargin=-10pt,
rightmargin=-10pt,
backgroundcolor=green!20,
linewidth=0pt
]{colorpar}
\usepackage{float}
\begin{document}
Some text of the main body of the document.
\begin{colorpar}
\paragraph{Paragraph}
Some text of the paragraph.
\begin{figure}[H]
\centering
\begin{tikzpicture}[tight background,
show background rectangle,
/tikz/background rectangle/.style={fill=white}
]
\node at (0,0) [rectangle,draw] {
\begin{tikzpicture}
\draw [->] (-.5,0) -- (5,0) node [below] {$x$};
\draw [->] (0,-.5) -- (0,3) node [left] {$y$};
\end{tikzpicture}};
\end{tikzpicture}
\caption{The rectangle only delimits the area without shaded background; it needn't be rendered in actual paragraph.}
\label{fig:label}
\end{figure}
Some text of the paragraph.
\end{colorpar}
Some text of the main body of the document.
\end{document}
答案2
默认情况下,所有TiKZ
图片都是“透明的”,也就是你所说的通常的白色背景不是白色,它看起来像白色,因为你把它们画在白色背景上。因此,当你把它们放在彩色背景上时,这个背景颜色会透过图形中所有未填充的部分显示出来。
借助backgrounds
库的帮助,可以轻松地为任何TiKZ
图形添加彩色背景,并且不需要将其包含在任何node
图形中surrounding
,只需向环境添加背景定义选项即可tikzpicture
。
下一个示例展示了如何做到这一点。它类似于 Karlkoeller 解决方案,但避免使用外部图形并使用tcolorbox
而不是mdframed
创建彩色段落。它还使用H
float
不浮动图形的选项。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usepackage{tcolorbox}
\usepackage{float}
\begin{document}
Some text of the main body of the document.
\begin{tcolorbox}[colback=blue!30]
\paragraph{Paragraph}
Some text of the paragraph.
\begin{figure}[H]
\centering
\begin{tikzpicture}[background rectangle/.style={draw, fill=white},
show background rectangle]
\draw [->] (-.5,0) -- (5,0) node [below] {$x$};
\draw [->] (0,-.5) -- (0,3) node [left] {$y$};
\fill[blue] (1,1) rectangle (2,2);
\end{tikzpicture}
\caption{The rectangle only delimits the area without shaded background; it needn't be rendered in actual paragraph.}
\label{fig:label}
\end{figure}
Some text of the paragraph. \\
\end{tcolorbox}
Some text of the main body of the document.
\end{document}