获取 Tikz 图片中最外层的坐标/长度

获取 Tikz 图片中最外层的坐标/长度

从我之前的问题/回答定义框内的 Tikz 图像(& textpos 包),@frougon 提供了一个很好的解决方案。现在,我正在寻求对我的所有 Tikz 图片进行标准缩放(请参阅编辑/添加)。

我希望所有 Tikz 图片的尺寸都是 3.5cm x 3.5cm,我可以使用 来实现scale = 3.5/k,其中k是 Tikz 图片的“坐标长度”。

我想知道是否有办法获取 Tikz 图片中最外层(或长度)的坐标?例如,

\begin{tikzpicture}
    \draw (1,4)--(10,7);
\end{tikzpicture}

将会有k=9,因为 x 轴的范围从 1 到 10(相差 9),而 y 轴的范围从 4 到 7(相差 3)。

我设想某种“最大”函数在这里发挥作用。类似于k = max(<largest x coordinate> - <smallest x coordinate >, <largest y coordinate > - <smallest y coordinate >) = max(10-1,7-4) = max(9,3) = 9

有关更详细的示例,请考虑以下内容:

\begin{tikzpicture}
    \draw[thick] (0,0)--(10,5);
    \draw[thick] (0,0)--(5,-10);
    \draw[thick] (0,0)--(-10,5);
    \draw[thick] (0,0)--(-5,-10);
    \draw[thick] (0,0)--(10,-5);
    \draw[thick] (0,0)--(-5,10);
    \draw (.5,2) node {1};
    \draw (-2.5,2.5) node {2};
    \draw (-2,-.5) node {3};
    \draw (0,-2.5) node {4};
    \draw (2.5,-2.5) node {5};
    \draw (2.5,0) node {6};
\end{tikzpicture}

在这种情况下,我想要k = 20,因为 x 轴(实际上也是 y 轴)的“长度”是 20 个坐标单位。

提前致谢!

答案1

更新:这将计算边界框的尺寸和相应的比例因子,并在下次运行中应用。(max由替换min,非常感谢 frougon!)

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\makeatletter
\newcommand\ExportBB[1]{\path let 
 \p1=($(current bounding box.north east)-(current bounding box.south west)$),
 \n1={#1/\x1},\n2={#1/\y1}
 in \pgfextra{\pgfmathsetmacro{\figscale}{min(\n1,\n2)}\xdef\figscale{\figscale}};
 \immediate\write\@mainaux{\xdef\string\figscale{\figscale}\relax}}
\makeatother
\tikzset{scale to max size/.style={execute at end picture={\ExportBB{#1}},
/utils/exec=\ifdefined\figscale
\else
\message{Recompile the figure.}
\xdef\figscale{1}
\fi,scale=\figscale}}
\begin{document}
\begin{tikzpicture}[scale to max size=3.5cm]
    \draw[thick] (0,0)--(10,5);
    \draw[thick] (0,0)--(5,-10);
    \draw[thick] (0,0)--(-10,5);
    \draw[thick] (0,0)--(-5,-10);
    \draw[thick] (0,0)--(10,-5);
    \draw[thick] (0,0)--(-5,10);
    \draw (.5,2) node {1};
    \draw (-2.5,2.5) node {2};
    \draw (-2,-.5) node {3};
    \draw (0,-2.5) node {4};
    \draw (2.5,-2.5) node {5};
    \draw (2.5,0) node {6};
\end{tikzpicture}
\end{document}

在此处输入图片描述

当然,文本不会被转换。您也可以转换它们,但使用\maxsizebox{3.5cm}{3.5cm}{....}附带的文本可能会更简单adjustbox

\n1这将在第一个示例中存储和中的尺寸\n2,并在第二个示例中在节点中打印尺寸。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
    \draw (1,4)--(10,7);
    \path let \p1=($(current bounding box.north east)-(current bounding
    box.south west)$),\n1={\x1/1cm},\n2={\y1/1cm}
    in \pgfextra{\typeout{x=\n1 cm,y=\n2 cm}};
\end{tikzpicture}

\begin{tikzpicture}[store dimensions of current picture in/.style 2 args={
  insert path={let \p1=($(current bounding box.north east)-(current bounding
    box.south west)$),\n1={\x1/1cm},\n2={\y1/1cm}
    in \pgfextra{\edef#1{\n1}\edef#2{\n2}}}}]
    \draw[thick] (0,0)--(10,5);
    \draw[thick] (0,0)--(5,-10);
    \draw[thick] (0,0)--(-10,5);
    \draw[thick] (0,0)--(-5,-10);
    \draw[thick] (0,0)--(10,-5);
    \draw[thick] (0,0)--(-5,10);
    \draw (.5,2) node {1};
    \draw (-2.5,2.5) node {2};
    \draw (-2,-.5) node {3};
    \draw (0,-2.5) node {4};
    \draw (2.5,-2.5) node {5};
    \draw (2.5,0) node {6};
    \path[store dimensions of current picture in={\myx}{\myy}]
    node[fill=white] at (current bounding box.center) {%
    $x=\pgfmathparse{\myx}\pgfmathprintnumber{\pgfmathresult}$cm,
    $y=\pgfmathparse{\myy}\pgfmathprintnumber{\pgfmathresult}$cm};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

在我看来这是一种XY 问题...我不确定你想要的确切语义,但例如:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{adjustbox}
\begin{document}
\adjustboxset{min width=3.5cm, min height=3.5cm, max width=3.5cm, max height=3.5cm}

\adjustbox{}{\tikz \draw (0,0) rectangle (4,6) (2,2) rectangle (3,3);}
\adjustbox{}{\tikz \draw (0,0) rectangle (6,4) (2,2) rectangle (3,3);}
\adjustbox{}{\tikz \draw (0,0) rectangle (6,6) (2,2) rectangle (3,3);}
\adjustbox{}{\tikz \draw (0,0) rectangle (1,1) (.2,.2) rectangle (.3,.3);}
\end{document}

将产生: 在此处输入图片描述

这可能是也可能不是你想要的(请注意缩放对较小图片的影响)。

否则,我看到的唯一其他方法是使用@marmot 回答,并做一些魔术来将比例因子存储在.aux文件中以供第二遍使用。(现在想起来太复杂了,没有时间......)

相关内容