为什么 pgf 不理解相对尺寸 (ex、em、sp)?

为什么 pgf 不理解相对尺寸 (ex、em、sp)?

下面的 MWE 使用低级pgf命令绘制了许多矩形,每个矩形都指定不同的Latex 理解的单位。但是,pgf似乎无法确定相对单位exemsp。这有什么原因吗?

就上下文而言,我试图声明一个相对于当前文本具有大小的新形状。由于该形状不包含任何文本,因此这似乎是一种比使其相对于不存在的更好的方法\pgfnodeparttextbox。如果pgf在这种情况下无法处理相对单位,是否有更好的方法来设置形状的初始大小?

在 pgf 中用不同单位绘制的框

\documentclass[a4paper]{article}
\usepackage{tikz}

\begin{document}

\newdimen\ptdim
\newdimen\pcdim
\newdimen\indim
\newdimen\bpdim
\newdimen\cmdim
\newdimen\mmdim
\newdimen\dddim
\newdimen\ccdim
\newdimen\spdim
\newdimen\exdim
\newdimen\emdim

\begin{tikzpicture}
  \ptdim=12pt
  \pcdim=2pc
  \indim=2in
  \bpdim=100bp
  \cmdim=3cm
  \mmdim=20mm
  \dddim=40dd
  \ccdim=10cc
  \spdim=500sp
  \exdim=5ex
  \emdim=5em
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\ptdim}{\ptdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\pcdim}{\pcdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\indim}{\indim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\bpdim}{\bpdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\cmdim}{\cmdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\mmdim}{\mmdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\dddim}{\dddim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\ccdim}{\ccdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\spdim}{\spdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\exdim}{\exdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\emdim}{\emdim}}
  \pgfusepath{stroke}
  \node[below] at (\ptdim,0) {pt};
  \node[below] at (\pcdim,0) {pc};
  \node[below] at (\indim,0) {in};
  \node[below] at (\bpdim,0) {bp};
  \node[below] at (\cmdim,0) {cm};
  \node[below] at (\mmdim,0) {mm};
  \node[below] at (\dddim,0) {dd};
  \node[below] at (\ccdim,0) {cc};
  \node[below] at (\spdim,0) {sp};
  \node[below] at (\exdim,0) {ex};
  \node[below] at (\emdim,0) {em};
\end{tikzpicture}
\end{document}

答案1

在 TikZ 图片中,\nullfont为了避免乱排版,一般会将当前字体设置为 ;该字体所有参数都为零,所以当字体为 时,1em = 1ex = 0pt \nullfont。另一方面,1sp 非常小,因为 65536sp=1pt,所以 500sp 的位移几乎无法体现出来。

在输入图片之前只需设置长度:

\documentclass[a4paper]{article}
\usepackage{tikz}

\begin{document}

\newdimen\ptdim
\newdimen\pcdim
\newdimen\indim
\newdimen\bpdim
\newdimen\cmdim
\newdimen\mmdim
\newdimen\dddim
\newdimen\ccdim
\newdimen\spdim
\newdimen\exdim
\newdimen\emdim

  \ptdim=12pt
  \pcdim=2pc
  \indim=2in
  \bpdim=100bp
  \cmdim=3cm
  \mmdim=20mm
  \dddim=40dd
  \ccdim=10cc
%  \spdim=500sp
  \exdim=5ex
  \emdim=5em
\begin{tikzpicture}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\ptdim}{\ptdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\pcdim}{\pcdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\indim}{\indim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\bpdim}{\bpdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\cmdim}{\cmdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\mmdim}{\mmdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\dddim}{\dddim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\ccdim}{\ccdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\spdim}{\spdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\exdim}{\exdim}}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\emdim}{\emdim}}
  \pgfusepath{stroke}
  \node[below] at (\ptdim,0) {pt\vphantom{pb}};
  \node[below] at (\pcdim,0) {pc\vphantom{pb}};
  \node[below] at (\indim,0) {in\vphantom{pb}};
  \node[below] at (\bpdim,0) {bp\vphantom{pb}};
  \node[below] at (\cmdim,0) {cm\vphantom{pb}};
  \node[below] at (\mmdim,0) {mm\vphantom{pb}};
  \node[below] at (\dddim,0) {dd\vphantom{pb}};
  \node[below] at (\ccdim,0) {cc\vphantom{pb}};
%  \node[below] at (\spdim,0) {sp};
  \node[below] at (\exdim,-0.5) {ex\vphantom{pb}};
  \node[below] at (\emdim,-0.5) {em\vphantom{pb}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

尝试执行以下操作

\setlength{\exdim}{5ex}
\setlength{\emdim}{5em}

它似乎对我有用。

相关内容