我的文档中有一些 Tikz 图形,我想在其周围绘制一个框架。此框架的宽度应始终为 \textwidth。高度应自动调整为实际 Tikz 对象的高度。是否有一个与 Tikz 对象大小无关的通用解决方案?
这是我第一次尝试,但没有成功。我怎样才能重叠红色和黑色的边框?(这只是一个说明问题的例子。通常黑色矩形是任意的 Tikz 对象。)
\documentclass{scrbook}
\usepackage{tikz}
\usepackage{blindtext}
\begin{document}
\blindtext
\begin{figure}[h]
\begin{tikzpicture}
\node(boundbox) [draw=red] {
\begin{tikzpicture} % could be an arbitrary complex Tikz object
\draw (0,0) rectangle (\textwidth,0.5);
\end{tikzpicture}
};
\end{tikzpicture}
\end{figure}
\blindtext
\end{document}
答案1
对于基本的解决方案,您可以使用(这将禁止节点中的逐字内容):
\framebox[\textwidth]{<TikZ picture>}
对于 TikZ 解决方案,backgrounds
库已经提供了一个show background rectangle
键(别名:)framed
。通过适当的inner frame xsep
设置,我们可以非常轻松地实现“框架框”。由于框架实际上增加了更多宽度(两侧线宽的一半),我们需要稍微改变背景路径的定义。
显然,我们希望将此校正直接添加到值的计算中inner frame xsep
,但我们不能保证它会得到正确的结果\pgflinewidth
(因为计算是在路径之外进行的)。我们显然可以对其进行硬编码(相关参考:[1]),但是我们不能对不同的框架使用不同的线宽(尽管我们可能不会(也不应该)这样做)。
或者,将线宽的一半伸入边距,
background rectangle/.append code={\tikz@addmode{\pgf@relevantforpicturesizefalse}}
代码
\documentclass{scrartcl}
\usepackage{tikz}
\usepackage[showframe,pass]{geometry}
\usetikzlibrary{backgrounds}
\makeatletter
\tikzset{inner frame xsep=.5\textwidth-.5\pgf@picmaxx+.5\pgf@picminx}
\def\tikz@background@framed{% just overwriting the original definition
\tikz@background@save%
\pgfonlayer{background}
\path[style=background rectangle] (\tikz@bg@minx+.5\pgflinewidth,\tikz@bg@miny) rectangle (\tikz@[email protected]\pgflinewidth,\tikz@bg@maxy);
\endpgfonlayer}
\makeatother
\newcommand*\arbitrarycomplexTikZobject[1][]{
\begin{tikzpicture}[#1] % could be an arbitrary complex Tikz object
\filldraw[opacity=.4] (0,0) rectangle node[text=white,scale=5,opacity=1] {Ti\emph{k}Z} (.5\textwidth,2cm);
\end{tikzpicture}}
\begin{document}
Space.
\begin{figure}[ht]
\framebox[\textwidth]{\arbitrarycomplexTikZobject}
\caption{Frame with \texttt{\string\framebox}.}
\end{figure}
\tikzset{every picture/.append style={framed}}
\begin{figure}[ht]
\arbitrarycomplexTikZobject
\caption{Frame with the \texttt{framed} option and a nice \texttt{inner frame xsep} setting.}
\end{figure}
\begin{figure}[ht]
\arbitrarycomplexTikZobject[background rectangle/.append style={ultra thick,draw=red,top color=blue,rounded corners}]
\caption{Frame with the \texttt{framed} option, a nice \texttt{inner frame xsep} setting and a badass frame which is taken from the Ti\emph{k}Z manual but without the \texttt{double} option.}
\end{figure}
\end{document}
输出
答案2
另一种构图方式是tcolorbox
。默认情况下,它将框宽度调整为\linewidth
。
\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{blindtext}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\begin{document}
\blindtext
\begin{figure}[h]
\begin{tcolorbox}\centering
\begin{tikzpicture}
\filldraw[opacity=.4] (0,0) rectangle node[text=white,scale=5,opacity=1] {Ti\emph{k}Z} (.5\textwidth,2cm);
\end{tikzpicture}
\end{tcolorbox}
\caption{\texttt{tcolorbox} example}
\end{figure}
\begin{figure}[h]
\begin{tcolorbox}[beamer,colback=red!5!white,colframe=red!75!black,]\centering
\begin{tikzpicture}
\filldraw[opacity=.4] (0,0) rectangle node[text=white,scale=5,opacity=1] {Ti\emph{k}Z} (.5\textwidth,2cm);
\end{tikzpicture}
\end{tcolorbox}
\caption{\texttt{tcolorbox} example}
\end{figure}
\end{document}
答案3
另一种解决方案是假设您的数字居中。
其想法是绘制一个空节点,并使用TikZ 绘图创建的库和边界框\textwidth
确定高度。fit
代码
\documentclass{scrbook}
\usepackage{tikz}
\usetikzlibrary{fit}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{figure}[h]
\begin{tikzpicture}
% arbitrary TikZ picture begins ---------------
\filldraw[opacity=.4] (0,0) rectangle node[text=white,scale=5,opacity=1] {Ti\emph{k}Z} (.5\textwidth,2cm);
% arbitrary TikZ picture ends -----------------
\node[
draw=red,
minimum width=\textwidth,
fit=(current bounding box.north west) (current bounding box.south east),
]at (current bounding box.center){};
\end{tikzpicture}
\caption{Figure Title}
\end{figure}
\lipsum[2]
\end{document}
输出
替代水平对齐
如果不想将 TikZ 图形对齐在中心,而是想将其左对齐(或右对齐),则只需将节点位置从 更改为at (current bounding box.center)
(at (current bounding box.east)
或at (current bounding box.west)
右对齐)。
\begin{figure}[h]
\begin{tikzpicture}
% arbitrary TikZ picture begins ---------------
\filldraw[opacity=.4] (0,0) rectangle node[text=white,scale=5,opacity=1] {Ti\emph{k}Z} (.5\textwidth,2cm);
% arbitrary TikZ picture ends -----------------
\node[
draw=red,
minimum width=\textwidth,
fit=(current bounding box.north west) (current bounding box.south east),
]at (current bounding box.east){};
\end{tikzpicture}
\caption{Figure Title}
\end{figure}