TikZ 和 PGF——设计考虑和调整图片大小的方法?

TikZ 和 PGF——设计考虑和调整图片大小的方法?

手册的前几页TikZ/PGF指定测量单位是点、厘米或与其他线/物体相关,但我不明白如何预先确定这一点。

针对我正在使用的一些图形,我有一个解决方案(由 Qrrbrbirlbel 提供):

在书中创建图形的方法——TikZ、SVG、PDF、Inkscape?

这正是我想要的,但我希望有更小的网格,并能够将它们放置在页面上(例如)5 列/8 行矩阵中。我假设有一些“自动”方法可以做到这一点,但从我从 TikZ 和 PGF 手册中学到的知识来看,似乎作者必须事先知道尺寸。

我知道有缩放选项,但由于我对 TikZ/PGF 的了解非常有限,它们似乎没有解决多张图片的缩放并将它们排列在纸上的网格中的问题。

TikZ如果不考虑图形尺寸,创建图形的方法是什么?

编辑以添加:我确实找到了\matrix手册中讨论这个问题的部分,所以也许这是解决它的方法?

答案1

我对链接的答案做了一些修改,现在您可以指定图片行数、每行图片数和间距。请注意,图片之间的间距被粗略地取消了,例如\hspace{-1.026cm},这可能对字体和字体大小敏感,可能需要调整(欢迎提出改进建议)。此外,由于精度有限,图片的高度并不完全相同,会有几个像素的差异。如果需要提高精度,可能需要浮点运算单元图书馆蒂克兹可以作为一种补救措施。否则,就不要把空间弄得太小,或者禁用该grayback命令。

代码

\documentclass{scrartcl}
\usepackage[inner=10mm,outer=35mm,top=20mm,bottom=30mm,twoside]{geometry}
\usepackage{tikz}
\usepackage{xifthen}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\newcommand{\getsizes}%
{   \path (current bounding box.south west);
  \pgfgetlastxy{\xsw}{\ysw}
  \path (current bounding box.north east);
  \pgfgetlastxy{\xne}{\yne}
  \pgfmathsetlengthmacro{\picwidth}{\xne-\xsw}
  \pgfmathsetlengthmacro{\picheight}{\yne-\ysw}
  \pgfmathsetmacro{\maxwidth}{(0.98*\textwidth-(\picturesperrow-1)*\pictureseparation*29)/\picturesperrow}
  \pgfmathsetmacro{\xscale}{\maxwidth/\picwidth}
  \pgfmathsetmacro{\maxheight}{(0.98*\textheight-(\picturerows-1)*\pictureseparation*29)/\picturerows}
  \pgfmathsetmacro{\yscale}{\maxheight/\picheight}
  \xdef\xscalefactor{\xscale}
  \xdef\yscalefactor{\yscale}
}

\newcommand{\grayback}%
{   \begin{pgfonlayer}{background}
  \fill[left color=gray!20,right color=gray!5,opacity=0.5] (current bounding box.south west) rectangle (current bounding box.north east);
  \fill[top color=gray!5,bottom color=gray!70,opacity=0.5] (current bounding box.south west) rectangle (current bounding box.north east);
    \end{pgfonlayer}
}


\newcommand{\adjustedtikzsize}[1]% draw commands
{   \stepcounter{galleryitem}
  \smash{\vphantom{
        \begin{tikzpicture}
        #1
      \getsizes
    \end{tikzpicture}
  }}
  \begin{tikzpicture}[xscale=\xscalefactor, yscale=\yscalefactor]
      #1
      \grayback
  \end{tikzpicture}
  \hspace{-1.026cm}
  \pgfmathtruncatemacro{\breaktest}{mod(\thegalleryitem,\picturesperrow)}
  \ifthenelse{\breaktest=0}{\par\vspace{-0.53cm}\vspace{\pictureseparation cm}}{\hspace{\pictureseparation cm}}
}

\newcounter{galleryitem}
\setcounter{galleryitem}{0}

\newenvironment{tikzgallery}[3]% pics per row, rows, space in cm
{\clearpage\xdef\picturesperrow{#1}\xdef\picturerows{#2}\xdef\pictureseparation{#3}\setcounter{galleryitem}{0}}%
{}

\newcommand{\picone}
{   \draw[rotate=30,left color=red,right color=blue] (0,0) rectangle (5,2);
}

\newcommand{\pictwo}
{   \draw[rotate=20,left color=green,right color=red] (0,0) rectangle (5,2);
}

\newcommand{\picthree}
{   \draw[rotate=40,left color=orange,right color=blue] (0,0) rectangle (5,2);
}

\newcommand{\picfour}
{   \draw[left color=black!10,right color=black!90] (-1,-7) rectangle (1,7);
    \draw[left color=yellow,right color=violet] (0,0) circle (5);
}

\begin{document}

\parindent0mm
\parskip0mm

\begin{tikzgallery}{4}{3}{0.1}% pics per row, rows, space in cm
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}    
\end{tikzgallery}

\begin{tikzgallery}{5}{7}{1}% pics per row, rows, space in cm
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picone} 
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}   
\end{tikzgallery}

\begin{tikzgallery}{2}{3}{0.5}% pics per row, rows, space in cm
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
\end{tikzgallery}

\begin{tikzgallery}{9}{9}{0.1}% pics per row, rows, space in cm
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picfour}
    \adjustedtikzsize{\picfour}    
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picthree}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\picone}
    \adjustedtikzsize{\pictwo}
    \adjustedtikzsize{\picone}
\end{tikzgallery}

\end{document}

输出

在此处输入图片描述

相关内容