自動將 tikzpicture 縮放到頁面上可能的最大尺寸

自動將 tikzpicture 縮放到頁面上可能的最大尺寸

是否可以tikz使用某些參數將圖形縮放到頁面上顯示的文字可能的最大尺寸?例如,是否有一些參數使得

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

我實現這一目標的唯一方法是反覆試驗。

我的意思是我嘗試

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

它適合頁面,我可以將其放大嗎?嘗試

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

太大了所以縮小了

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

好吧,它又適合了。我可以把它弄大一點嗎?

... ETC。

Latex 有沒有一種簡單的方法可以將圖片自動調整到目前頁面上可能存在的所有文字、其他圖形等的最大尺寸?

答案1

插入的數字在某種程度上是「固定的」。所以我不使用figure環境或其他float環境。

想法很簡單:計算頁面的剩餘高度

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

center預設情況下,#1 是環境 和 的caption保留空間4\baselineskip

然後將 適合width和 heighttikzpicture的框框。\textwidth\dimexpr\pagegoal-\pagetotal-#1\relax

  • \getpicdimen用於取得圖片的原始尺寸。
  • \fittobox用於將圖片適合固定寬度和高度的框框。
  • \fitremaining用於使圖片適合頁面的剩餘空間。
\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}

在此輸入影像描述 在此輸入影像描述

相關內容