大型 TikZ 图片缩放到页面 - 第 2 部分

大型 TikZ 图片缩放到页面 - 第 2 部分

我需要绘制房屋平面图并使其适合页面。我非常希望在现实生活中和我的 TeX 文件中都有一组尺寸/坐标(我过去尝试过一些不规则的缩放,即 TeX 文件中的一个尺寸和现实生活中的其他尺寸,但这样做不行)。

有没有办法解决这个错误?谢谢!(请在图纸和网格中标注 50m x 50m)

[Loading MPS to PDF converter (version 2006.09.02).]
)
! Dimension too large.
<to be read again> 
                   \relax 
l.28   \end{scaletikzpicturetowidth}

MWE(Miktex 2.9.4248 x64;pdfTeX,版本 3.1415926-2.5-1.40.14):

\documentclass[fleqn]{article}
\usepackage{tikz}
\usepackage{pdflscape}
\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{center}
  \begin{scaletikzpicturetowidth}{\textwidth}
    \begin{tikzpicture}[scale=\tikzscale]
      \draw [step=10cm, lightgray, very thin] (0,0) grid  (50000mm,50000mm);
      \draw [draw=black, thin] (0.0mm, 0.0mm) rectangle ++(50000mm,50000mm) node[pos=.5] {some labels};
    \end{tikzpicture}
  \end{scaletikzpicturetowidth}
\end{center}
\end{document}

答案1

  • 使用无量纲坐标。
  • [x=0.005pt,y=0.005pt]通过向 中添加选项来指定单位长度tikzpicture

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{center}
  \begin{tikzpicture}[x=0.005pt,y=0.005pt]
    \draw [step=100, lightgray, very thin] (0,0) grid  (50000,50000);
    \draw [draw=black, thin] (0.0, 0.0) rectangle ++(50000,50000) node[pos=.5] {some labels};
  \end{tikzpicture}
\end{center}
\lipsum[2]
\end{document}

自动将图片缩放到给定的宽度:原始帖子的环境scaletikzpicturetowidth(基于Philippe Goudet 的回答,这又基于Ulrike Fischer 在 comp.text.tex 上提供的解决方案) 可以适应这种方法。在序言中添加以下几行:

\usepackage{environ}
\newsavebox{\measuretikzpicture}
\NewEnviron{scaletikzpicturetowidth}[1]{%
  \def\tikzscale{0.05}% choose small enough to avoid overflows, but large enough to minimize rounding errors
  \savebox{\measuretikzpicture}{\BODY}%
  \pgfmathparse{#1/\wd\measuretikzpicture*\tikzscale}%
  \edef\tikzscale{\pgfmathresult}%
  \BODY
}

理论上,0.05此定义中的魔法数字可以是任何数字,例如原始帖子中的 1。然而,实际上,TeX 中的数字被限制在一个很小的范围内,因此\tikzscale必须足够小,以免发生溢出(如原始帖子中所述),但也要足够大,以使舍入误差可以忽略不计。您会注意到 TeX 溢出会给出错误消息,而严重的舍入误差会导致缩放不充分。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usepackage{environ}
\newsavebox{\measuretikzpicture}
\NewEnviron{scaletikzpicturetowidth}[1]{%
  \def\tikzscale{0.05}% choose small enough to avoid overflows, but large enough to minimize rounding errors
  \savebox{\measuretikzpicture}{\BODY}%
  \pgfmathparse{#1/\wd\measuretikzpicture*\tikzscale}%
  \edef\tikzscale{\pgfmathresult}%
  \BODY
}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{center}
  \begin{scaletikzpicturetowidth}{\textwidth}%
    \begin{tikzpicture}[x=\tikzscale pt,y=\tikzscale pt]
      \draw [step=100, lightgray, very thin] (0,0) grid  (50000,20000);
      \draw [draw=black, thin] (0.0, 0.0) rectangle ++(50000,20000) node[pos=.5] {some labels};
    \end{tikzpicture}%
  \end{scaletikzpicturetowidth}
\end{center}
Same picture, but smaller by a factor of 10 (50000 replaced by 5000), again scaled to the text width.
\begin{center}
  \begin{scaletikzpicturetowidth}{\textwidth}%
    \begin{tikzpicture}[x=\tikzscale pt,y=\tikzscale pt]
      \draw [step=100, lightgray, very thin] (0,0) grid  (5000,2000);
      \draw [draw=black,thin] (0.0, 0.0) rectangle ++(5000,2000) node[pos=.5] {some labels};
    \end{tikzpicture}%
  \end{scaletikzpicturetowidth}
\end{center}
\lipsum[2]
\end{document}

相关内容