4

Is it possible to scale a tikz figure to the maximum size possible for the page with the text that is present on it using some parameters? For example, are there some parameters such that

\usepackage{float}
\usepackage{tikz, pgfplots} 

\begin{document}

\begin{figure}[H]
\begin{tikzpicture}[scale = **SOMETHING_HERE_TO_AUTO_SCALE_TO_MAX_SIZE_ON_PAGE?**]
% ... code to draw figure
\end{tikzpicture}
\end{figure}

\end{document}

The only way I have been achieving this is by trial and error.

By this I mean I try

\begin{figure}[H]
\begin{tikzpicture}[scale = 1]
% ... code to draw figure
\end{tikzpicture}
\end{figure}

It fits on the page so can I make it bigger? Try

\begin{figure}[H]
\begin{tikzpicture}[scale = 2]
% ... code to draw figure
\end{tikzpicture}
\end{figure}

It's too big, so I scale down

\begin{figure}[H]
\begin{tikzpicture}[scale = 1.5]
% ... code to draw figure
\end{tikzpicture}
\end{figure}

Ok it fits again. Can I make it bigger?

... etc.

Is there an easy way for latex to auto adjust the picture to the maximum size possible on the current page with all text, other figures etc. that are already present?

4
  • There is no way to do this with the scale option. You can use \resizebox{\textwidth}{!}{\begin{tikzpicture}...\end{tikzpicture}} but that will scale the text as well. Commented Sep 13, 2020 at 22:00
  • This scales everything to the text width and caused the image to go to the next page. I would like the image to remain on the same page that it is inserted and maximize the fit to to the amount of free space available.
    – NM_
    Commented Sep 13, 2020 at 22:09
  • Then you can try \maxsizebox*{\textwidth}{<max height>}{\begin{tikzpicture}...\end{tikzpicture}} where you replace <max height> with whatever is the maximum height that you want to have. Needs \usepackage{adjustbox}. For <max height> you can use \dimexpr\pagegoal-\pagetotal-1cm\relax where \pagegoal-\pagetotal is roughly the space left on the page and the -1cm is to account for inaccuracies. Commented Sep 13, 2020 at 22:32
  • See tex.stackexchange.com/questions/485318/… Commented Sep 14, 2020 at 14:34

1 Answer 1

1

Inserted figure is "fixed" to some extent. So I do not use figure environment or other float environment.

The idea is simple: calculate the remaining height of the page

\dimexpr\pagegoal-\pagetotal-#1\relax

#1 is the reserved space for center environment and caption, 4\baselineskip by default.

Then fit the tikzpicture to the box of width \textwidth and height \dimexpr\pagegoal-\pagetotal-#1\relax.

  • \getpicdimen is used to get the original size of the pic.
  • \fittobox is used to fit the pic to box of fixed width and height.
  • \fitremaining is used to fit the pic to the remaining space of the page.
\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\usepackage{showframe}
\usepackage{lipsum}
\usetikzlibrary{fit, calc, positioning}
\usepackage{xparse}

\renewcommand*\ShowFrameColor{\color{red}}

\NewDocumentCommand { \getpicdimen } { s O{\picwidth} O{\picheight} +m }
  {
    \begin{pgfinterruptboundingbox}
    \begin{scope}[local bounding box=pic, opacity=0]
      \IfBooleanTF {#1}
        { \node[inner sep=0pt, fit=(#4)] {}; }
        { #4 }
    \end{scope}
    \path ($(pic.north east)-(pic.south west)$);
    \end{pgfinterruptboundingbox}
    \pgfgetlastxy{#2}{#3}
  }

\ExplSyntaxOn
\fp_new:N \l__scale_fp
\NewDocumentCommand { \fittobox } { O{\picwidth} O{\picheight} m m D(){0, 0} +m }
  {
    \getpicdimen[#1][#2]{#6}
    \fp_compare:nTF
      {
        % pic ratio
        \dim_ratio:nn { #1 } { #2 } >
        % box ratio
        \dim_ratio:nn { #3 } { #4 }
      }
      { \fp_set:Nn \l__scale_fp { \dim_ratio:nn { #3 } { #1 } } }
      { \fp_set:Nn \l__scale_fp { \dim_ratio:nn { #4 } { #2 } } }
    \begin{scope}[
      shift={($(#5) - \fp_use:N \l__scale_fp*(pic.center)$)},
      scale=\fp_use:N \l__scale_fp,
      nodes={transform~shape},
      ]
      #6
    \end{scope}
  }

\NewDocumentCommand { \fitremaining } { O{ 4\baselineskip } +m }
  {
    \fittobox{\textwidth}{\dimexpr\pagegoal-\pagetotal-#1\relax}{#2}
  }
\ExplSyntaxOff

\begin{document}
\lipsum[1]
\begin{center}
\begin{tikzpicture}
\fitremaining{
  \draw (0, 0) node[left, draw] {$A$} -- (1, 1) node[right, draw] {$B$};
}
\end{tikzpicture}
\captionof{figure}{Test figure}
\end{center}

Some other text.
\clearpage

\lipsum[1-3]
\begin{center}
\begin{tikzpicture}
\fitremaining{
  \draw (0, 0) node[left, draw] {$A$} -- (1, 1) node[right, draw] {$B$};
}
\end{tikzpicture}
\captionof{figure}{Test figure}
\end{center}
\clearpage
\end{document}

enter image description here enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .