TransWikia.com

Autoscale tikzpicture to maximum size possible on a page

TeX - LaTeX Asked on July 11, 2021

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?

One Answer

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

dimexprpagegoal-pagetotal-#1relax

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

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

  • 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{ 4baselineskip } +m }
  {
    fittobox{textwidth}{dimexprpagegoal-pagetotal-#1relax}{#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

Correct answer by ZhiyuanLck on July 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP