tcolorbox
可以\tcbuselibrary{raster}
分配框来填充页面的空白。之后我希望我的 tikz 图像填充框的空白。框内容的宽度由给出,\linewidth
但框中可用空间的高度如何?
如果盒子的高度是通过计算的tcolorbox
。我怎样才能将值传递/tcb/text height
给tikzscale
然后能够生成我的图?
\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikzscale}
\usepackage{tcolorbox}
\tcbuselibrary{raster}
\begin{filecontents}{tikzimage.tikz}
\begin{tikzpicture}
\begin{axis}[xlabel=time,ylabel=value]
\addplot{x^2};
\end{axis}
\end{tikzpicture}
\end{filecontents}
\begin{document}
\begin{tcbraster}[%
raster columns=2,
raster rows=2,
raster height=\textheight,
]
\begin{tcolorbox}\end{tcolorbox}
\begin{tcolorbox}\end{tcolorbox}
\begin{tcolorbox}\end{tcolorbox}
\begin{tcolorbox}[title={box 1\\line two}]
\includegraphics[%
width=\linewidth,
height=4cm, % how to get the height of the actual box here?
]{tikzimage.tikz}
\end{tcolorbox}
\end{tcbraster}
\end{document}
答案1
对于tcolorbox
版本3.90 (2016/02/29)
,有一个属性\tcbtextheight
可以保存固定高度框的文本高度(受此问题启发!)。有了它,解决方案就很简单了:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikzscale}
\usepackage{tcolorbox}
\tcbuselibrary{raster}
\begin{filecontents}{tikzimage.tikz}
\begin{tikzpicture}
\begin{axis}[xlabel=time,ylabel=value]
\addplot{x^2};
\end{axis}
\end{tikzpicture}
\end{filecontents}
\begin{document}
\begin{tcbraster}[%
raster columns=2,
raster rows=2,
raster height=\textheight,
]
\begin{tcolorbox}\end{tcolorbox}
\begin{tcolorbox}\end{tcolorbox}
\begin{tcolorbox}\end{tcolorbox}
\begin{tcolorbox}[title={box 1\\line two}]
\includegraphics[%
width=\linewidth,
height=\tcbtextheight,
]{tikzimage.tikz}
\end{tcolorbox}
\end{tcbraster}
\end{document}
旧答案(适用于tcolorbox
之前版本3.90
):
在我的回答中,构建了一个新选项remember height
,将实际内部高度保存到上部框开头的选定宏中。以下示例使用\myheight
它来缩放包含的图像。
\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikzscale}
\usepackage{tcolorbox}
\tcbuselibrary{raster}
\begin{filecontents}{tikzimage.tikz}
\begin{tikzpicture}
\begin{axis}[xlabel=time,ylabel=value]
\addplot{x^2};
\end{axis}
\end{tikzpicture}
\end{filecontents}
\makeatletter
\tcbset{%
remember height/.style={before upper={%
\iftcb@fixedheight%
\tcbdimto#1{\kvtcb@top@rule@stand+\kvtcb@bottom@rule@stand+\kvtcb@boxsep*2+\kvtcb@top+\kvtcb@bottom}%
\iftcb@hasTitle%
\tcbdimto#1{#1+\ht\tcb@titlebox+\dp\tcb@titlebox+\kvtcb@title@rule+\kvtcb@boxsep*2+\kvtcb@toptitle+\kvtcb@bottomtitle}%
\fi%
\tcbdimto#1{\kvtcb@height@fixed-#1}%
\else%
\tcbdimto#1{4cm}% fallback
\fi%
}},
}
\makeatother
\begin{document}
\begin{tcbraster}[%
raster columns=2,
raster rows=2,
raster height=\textheight,
]
\begin{tcolorbox}\end{tcolorbox}
\begin{tcolorbox}\end{tcolorbox}
\begin{tcolorbox}\end{tcolorbox}
\begin{tcolorbox}[title={box 1\\line two},remember height=\myheight]
\includegraphics[%
width=\linewidth,
height=\myheight,
]{tikzimage.tikz}
\end{tcolorbox}
\end{tcbraster}
\end{document}
也许,这样的选择或者类似的东西作为官方选择也不错?
答案2
我找到了一种计算没有标题的框高度的方法,但没有找到有标题的框。 \tcb@upperbox
已定义但未包含任何内容。我怀疑下方的框是先创建的。
\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikzscale}
\usepackage{tcolorbox}
\tcbuselibrary{raster}
\makeatletter
\newcommand{\tcbheight}{\the\dimexpr\tcb@raster@box@height-\kvtcb@raster@skip@before-\kvtcb@raster@skip@after}
\makeatother
\begin{filecontents}{tikzimage.tikz}
\begin{tikzpicture}
\begin{axis}[xlabel=time,ylabel=value]
\addplot{x^2};
\end{axis}
\end{tikzpicture}
\end{filecontents}
\begin{document}
\begin{tcbraster}[%
raster columns=2,
raster rows=2,
raster height=\textheight,
]
\begin{tcolorbox}\rule{\linewidth}{1pt}\end{tcolorbox}
\begin{tcolorbox}\rule{1pt}{\tcbheight} \end{tcolorbox}
\begin{tcolorbox} (\tcbheight) \end{tcolorbox}
\begin{tcolorbox}[title={box 1\\line two}]
\includegraphics[%
width=\linewidth,
height=4cm, % how to get the height of the actual box here?
]{tikzimage.tikz}
\end{tcolorbox}
\end{tcbraster}
\end{document}