看起来,naiveline width=2\pgflinewidth
给出的线宽是正常线宽的四倍。这是为什么呢?
我怎样才能\draw
使用正常线宽的两倍( \pgflinewidth
),而无需创建新的长度?我也不希望sqrt(2)
以任何方式使用。
\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}[font=\tiny]
\draw (0pt,0pt) -- (10pt,0pt) node[right]{thin};
\draw[line width=2\pgflinewidth] (0pt,-5pt) -- (10pt,-5pt) node[right]{2*\textbackslash pgflinewidth};
\newlength\lw
\setlength\lw{\pgflinewidth}
\draw[line width=2\lw] (0pt,-10pt) -- (10pt,-10pt) node[right]{2*\textbackslash lw};
\end{tikzpicture}
\end{document}
答案1
为了原因密钥line width
有两个作用:
- 它通过(本地)设置
\pgflinewidth
为其参数。\pgfmathsetlength
- 它会添加到
\pgfsetlinewidth{#1}
路径最终确定时(在)执行的选项堆栈中;
。
(PGF 全球化,\pgflinewidth
因为它应该只按pgfscope
环境分组,并且 TikZ 针对每个环境都解决这个问题\path
。)
line width
这就是为什么设置依赖项会将其加倍的原因,\pgflinewidth
因为它基本上
\pgflinewidth = 2 * \pgflinewidth
\pgflinewidth = 2 * \pgflinewidth
我建议\pgflinewidth
在将其交给之前先扩大其价值line width
:
line width/.expanded = 2 * \the\pgflinewidth
如果需要更频繁地对线宽进行简单的分解,我建议line width *
定义如下键:
\tikzset{line width */.style={line width/.expanded={(#1)*\the\pgflinewidth}}}
可以像那样使用,line width *=2
而不必处理处理程序和\the
。
代码
\documentclass[tikz, border=1cm]{standalone}
\tikzset{line width */.style={line width/.expanded={(#1)*\the\pgflinewidth}}}
\begin{document}
\begin{tikzpicture}[font=\tiny]
\draw (0pt,0pt) -- (10pt,0pt) node[right]{thin};
\draw[line width *=2] (0pt,-5pt)
-- (10pt,-5pt) node[right]{2*\textbackslash pgflinewidth};
\newlength\lw
\setlength\lw{\pgflinewidth}
\draw[line width=2\lw] (0pt,-10pt)
-- (10pt,-10pt) node[right]{2*\textbackslash lw};
\end{tikzpicture}
\end{document}
输出
答案2
我认为如果不预先存储当前线宽,就无法完全解决这个问题。我不知道 Ti 在哪个时间点钾Z 设置线宽,但似乎您需要\pgflinewidth
在调用选项之前存储的值line width
,因为这反过来会更新的值\pgflinewidth
。您可以执行以下操作,这是更 Ti钾您的解决方案的 Z 类变体:
\documentclass[tikz, border=1cm]{standalone}
\newlength{\templinewidth}
\begin{document}
\begin{tikzpicture}
[font=\tiny, store line width/.code={\pgfmathsetlength{\templinewidth}{#1}}]
\draw[store line width={\pgflinewidth}, line width=\templinewidth]
(0pt,0pt) -- (10pt,0pt) node[right]{\textbackslash pgflinewidth};
\draw[store line width={\pgflinewidth}, line width={2\templinewidth}]
(0pt,-5pt) -- (10pt,-5pt) node[right]{2*\textbackslash pgflinewidth};
\end{tikzpicture}
\end{document}
当然,您也可以利用底层 PGF 层并直接在那里更改线宽,但这会全局改变线宽:
\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
[font=\tiny, multiply line width/.code={\pgfsetlinewidth{#1*\pgflinewidth}}]
\draw (0pt,0pt) -- (10pt,0pt) node[right]{\textbackslash pgflinewidth};
\draw[multiply line width={2}]
(0pt,-5pt) -- (10pt,-5pt) node[right]{2*\textbackslash pgflinewidth};
\draw (0pt,-10pt) -- (10pt,-10pt) node[right]{still 2*\textbackslash pgflinewidth};
\end{tikzpicture}
\end{document}
因此,您可能需要使用这种改进的方法,首先将当前线宽存储在 PGF 键中,然后使用此键设置实际线宽:
\documentclass[tikz, border=1cm]{standalone}
\tikzset{
stored line width/.initial=0pt,
store current line width/.code={
\pgfkeys{/tikz/stored line width/.expanded=\the\pgflinewidth},
},
multiply line width/.style={
store current line width,
line width={#1*\pgfkeysvalueof{/tikz/stored line width}}
}
}
\begin{document}
\begin{tikzpicture}[font=\tiny]
\draw (0pt,0pt) -- (10pt,0pt) node[right]{\textbackslash pgflinewidth};
\draw[multiply line width={2}]
(0pt,-5pt) -- (10pt,-5pt) node[right]{2*\textbackslash pgflinewidth};
\draw (0pt,-10pt) -- (10pt,-10pt) node[right]{\textbackslash pgflinewidth};
\end{tikzpicture}
\end{document}