
É possível dimensionar uma tikz
figura para o tamanho máximo possível para a página com o texto que nela está presente usando alguns parâmetros? Por exemplo, existem alguns parâmetros que
\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}
A única maneira de conseguir isso é por tentativa e erro.
Com isso quero dizer que eu tento
\begin{figure}[H]
\begin{tikzpicture}[scale = 1]
% ... code to draw figure
\end{tikzpicture}
\end{figure}
Cabe na página, posso aumentá-lo? Tentar
\begin{figure}[H]
\begin{tikzpicture}[scale = 2]
% ... code to draw figure
\end{tikzpicture}
\end{figure}
É muito grande, então eu diminuo
\begin{figure}[H]
\begin{tikzpicture}[scale = 1.5]
% ... code to draw figure
\end{tikzpicture}
\end{figure}
Ok, cabe novamente. Posso torná-lo maior?
... etc.
Existe uma maneira fácil para o látex ajustar automaticamente a imagem para o tamanho máximo possível na página atual com todo o texto, outras figuras, etc., que já estão presentes?
Responder1
A figura inserida é "fixa" até certo ponto. Então eu não uso figure
ambiente ou outro float
ambiente.
A ideia é simples: calcular a altura restante da página
\dimexpr\pagegoal-\pagetotal-#1\relax
#1 é o espaço reservado para center
ambiente e caption
, 4\baselineskip
por padrão.
Em seguida, ajuste tikzpicture
a caixa de largura \textwidth
e altura \dimexpr\pagegoal-\pagetotal-#1\relax
.
\getpicdimen
é usado para obter o tamanho original da foto.\fittobox
é usado para ajustar a foto a uma caixa de largura e altura fixas.\fitremaining
é usado para ajustar a foto ao espaço restante da página.
\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}