在下面的代码中,我将图像宽度的水平单位设置tikzpicture
为1/20
。我还想使用\parbox
该单位的小数倍作为其宽度。
\documentclass[tikz,10pt,dvipsnames,border=0cm]{standalone}
\usetikzlibrary{patterns}
\def\M{10}% columns
\def\N{10}% rows
\def\scale{1}% scale
\def\filename{example-image-a.pdf}% filename
\def\mygrid{%
\draw[help lines,red,step=.1,ForestGreen!50](-\M,-\N) grid (\M,\N);
\draw[help lines,red,step=1](-\M,-\N) grid (\M,\N);
\foreach \x in{-\M,...,\M}{\node[anchor=south] at (\x,\N){\x};}
\foreach \y in{-\N,...,\N}{\node[anchor=east] at (-\M,\y){\y};}
}
\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics[scale=\scale,page=1]{\filename}}
\newif\ifgrid
\gridtrue
\gridfalse
\begin{document}
\begin{tikzpicture}[inner sep=0,x=.5\wd\IBox/\M\relax,y=.5\ht\IBox/\N\relax]
\node (image) at (0,0) {\usebox\IBox};
\tikzset{anchor=base west}
\node at (-8,1.6) {\parbox{4cm}{\color{red} In PSTricks we trust. Why?}}; % I want 4 times of the horizontal unit instead of 4 cm.
\ifgrid
\mygrid
\fi
\end{tikzpicture}
\end{document}
如何做?
答案1
这似乎可以解决问题(需要calc
tikz 库):
\path let \p1 = (4,0) in node at (-8,1.6) {\parbox{\x1}{\color{red} In PSTricks we trust. Why?}};
它使用let
绑定\p1
到点的运算来测量“4×(x 单位)” (4,0)
(请注意没有维度单位)。然后我们通过这样做来提取其 x 分量,\x1
从而扩展到正确的维度。
直接使用 PGF 可以获得类似的结果:
\newdimen\myunit
\pgfextractx{\myunit}{\pgfpointxy{1}{0}}
\node at (-8,1.6) {\parbox{4\myunit}{\color{red} In PSTricks we trust. Why?}};