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}

わかりました。またぴったりになりました。もっと大きくしてもいいですか?

...などなど

すでに存在するすべてのテキスト、その他の図などを使用して、LaTeX で画像を現在のページで可能な最大サイズに自動調整する簡単な方法はありますか?

答え1

挿入した図形はある程度「固定」されますので、figure環境などは使いませんfloat

アイデアはシンプルです。ページの残りの高さを計算します。

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

#1 は、デフォルトでcenter環境と用に予約されたスペースです。caption4\baselineskip

次に、幅と高さtikzpictureのボックスに を合わせます。\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}

ここに画像の説明を入力してください ここに画像の説明を入力してください

関連情報