使 tikzpicture 适合页面宽度或高度(使用宏)

使 tikzpicture 适合页面宽度或高度(使用宏)

我定义了一个新的环境,使 tikzpicture 适合页面宽度,但是当 tikzpicture 的高度很长时,tikzpicture 的底部会被切割,并且由于 tikzpicture 跳转到下一页而出现额外的空白页。 在此处输入图片描述

我想找到标度宽度尺度高度参数(分别是适合页面宽度的缩放比例和适合页面高度的缩放比例),并选择较小的一个进行缩放,以保证 tikzpicture 的完整外观。

你能告诉我如何修改scaletikzpicturetowidth环境来实现这一点吗?

  \NewEnviron{scaletikzpicturetowidth}[1]{%
  \def\tikz@width{#1}     % what does this do?
  \def\tikzscale{1}   %declares tikzscale parameter 
  \begin{lrbox}{\measure@tikzpicture}    % BODY is saved in measure@tikzpicture
      \BODY
  \end{lrbox}%
  \pgfmathparse{#1/\wd\measure@tikzpicture}    % a scale ratio = page_width/tikzpic_width
  \edef\tikzscale{\pgfmathresult}  % tikzscale = a scale ration 
  \BODY
}

因此,环境将采用两个参数:

#1 = \textwidth#2 = \textheight

然后计算

widthscale = #1/\wd\measure@tikzpicture

heightsacle = #2/\ht\measure@tikzpicture, 和

\tikzscale = min(widthscale, heightsacle)

\documentclass[landscape]{article}
\setlength\textwidth{10in}
\setlength\textheight{8.5in}
\setlength\oddsidemargin{-.5in}
\setlength\evensidemargin{-.5in}
\setlength\topmargin{-.7in}
\setlength\headsep{0in}
\setlength\headheight{0in}
\setlength\topskip{0in}
\usepackage{tikz-qtree-compat}
\usepackage{environ}
\makeatletter
\newsavebox{\measure@tikzpicture}
\NewEnviron{scaletikzpicturetowidth}[1]{%
  \def\tikz@width{#1}%
  \def\tikzscale{1}\begin{lrbox}{\measure@tikzpicture}%
  \BODY
  \end{lrbox}%
  \pgfmathparse{#1/\wd\measure@tikzpicture}%
  \edef\tikzscale{\pgfmathresult}%
  \BODY
}
\makeatother
\begin{document}
  \begin{scaletikzpicturetowidth}{\textwidth}
  \begin{tikzpicture}[scale=\tikzscale, baseline=0pt, grow=down]
    \tikzset{level distance = 25pt, sibling distance = -5pt}
    \tikzset{every tree node/.style={align=center,anchor=north}}
    \Tree
      [. \node{ $s$};
        [. \node{\begin{tabular}{c}
                \texttt{run}\\
                \textbf{run}\\
                \normalsize{na}\\
                \scriptsize{na}\\
                \scriptsize{na}\\
                \end{tabular} }; ]
        [. \node{\begin{tabular}{c}
             \textbf{$X_{3109}$}\\
           \end{tabular} };
        ]
      ]
  \end{tikzpicture}
  \end{scaletikzpicturetowidth}
  \clearpage
  \begin{scaletikzpicturetowidth}{\textwidth}
  \begin{tikzpicture}[scale=\tikzscale, baseline=0pt, grow=down]
    \tikzset{level distance = 25pt, sibling distance = -5pt}
    \tikzset{every tree node/.style={align=center,anchor=north}}
    \Tree
      [. \node{ $np$};
        [. \node{\begin{tabular}{c}
                \texttt{some}\\
                \textbf{some}\\
                \normalsize{na}\\
                \scriptsize{na}\\
                \scriptsize{na}\\
                \end{tabular} }; ]
        [. \node{\begin{tabular}{c}
                \texttt{girl}\\
                \textbf{girl}\\
                \normalsize{na}\\
                \scriptsize{na}\\
                \scriptsize{na}\\
                \end{tabular} }; ]
      ]
  \end{tikzpicture}
  \end{scaletikzpicturetowidth}
  \clearpage
\end{document}

答案1

您可能可以使用 tikz 内部比例来做到这一点,但这很快并且适用于任何事物。

\documentclass{article}
\usepackage{tikz}
\usepackage{graphics}

\newlength{\xsize}
\newlength{\ysize}
\newsavebox{\tempbox}

\newcommand{\maxsize}[1]% #1=image to be scaled
{\savebox{\tempbox}{#1}%
\settoheight{\ysize}{\usebox{\tempbox}}%
\settowidth{\xsize}{\usebox{\tempbox}}%
\pgfmathparse{\xsize/\textwidth > \ysize/\textheight}%
\noindent%
\if\pgfmathresult1\resizebox{\textwidth}{!}{\usebox{\tempbox}}%
\else\centering\resizebox{!}{\textheight}{\usebox{\tempbox}}%
\fi}

\begin{document}
\vspace*{-\baselineskip}% first page only
\maxsize{\rule{3in}{1in}}

\maxsize{\rule{1in}{5in}}

\end{document}

进一步的研究表明,第一页的问题来自 \resizebox 中的舍入误差。因此我修改了解决方案,使其更加稳健。它最初有额外的代码来测试和截断 \MaxScale,但事实证明这是不必要的。

\documentclass{article}
\usepackage{tikz}
\usepackage{graphics}

\newlength{\MaxWidth}
\newlength{\MaxHeight}
\newsavebox{\MaxBox}

\newcommand{\maxsize}[1]% #1=image to be scaled
{\savebox{\MaxBox}{#1}% get size of box
\settoheight{\MaxHeight}{\usebox{\MaxBox}}%
\settodepth{\MaxWidth}{\usebox{\MaxBox}}%
\addtolength{\MaxHeight}{\MaxWidth}%
\settowidth{\MaxWidth}{\usebox{\MaxBox}}%
\pgfmathdivide{\textwidth}{\MaxWidth}% compute scale for width
\let\MaxScale=\pgfmathresult%
\ifdim\MaxScale\MaxHeight>\textheight% compute scale for height
\pgfmathdivide{\textheight}{\MaxHeight}
\let\MaxScale=\pgfmathresult%
\fi%
\noindent\centering\scalebox{\MaxScale}{\usebox{\MaxBox}}}

\begin{document}

\maxsize{\rule{1in}{5in}}

\maxsize{\rule{3in}{1in}}

\end{document}

相关内容