6

tcolorbox with \tcbuselibrary{raster} can distribute boxes to fill the hole page. After that I want my tikz image to fill the hole box. The width of the box content is given by \linewidth but how about the height of the available space in the box?

If the height of the box is calculated by tcolorbox. How can I pass the value /tcb/text height to tikzscale that is then able to generate my plot?

enter image description here

\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}
Tobias
  • 928
  • Are you saying you want to distort your image (i.e. alter it's aspect ratio) to fill the area? I don't think you need all the tikz image, within a filecontents, within an includegraphics within a tcolorbox palava. Even if you alter the height of the thing in your code it is not going to achieve it. – Aubrey Blumsohn Jan 20 '16 at 16:28
  • This is a minimal example. filecontents just to simplify. I use externalize and plots with lots of data loaded with pgfplotstable. I just want so seperate one single problem. – Tobias Jan 21 '16 at 20:31

2 Answers2

8

With tcolorbox version 3.90 (2016/02/29), there is a property \tcbtextheight which holds the text height of a fixed height box (inspired by this question!). With it, the solution is short:

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

Old Answer (valid for tcolorbox before version 3.90):

In my answer, a new option remember height is constructed which saves the actual inner height into a chosen macro at the begin of the upper box. The following example uses \myheight which can be used to scale the included image.

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

enter image description here

Maybe, such on option or something similar whould not be bad as an official option?

3

I found a way to compute the height of a box without a title, but not with one. \tcb@upperbox is defined but contains nothing. I suspect the lower box is created first.

\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}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120