我用 pgf 创建了一个形状,我想添加一个透明度选项(到我的形状的一部分)。我用 和 更改不透明度\pgfsetstrokeopacity
。\pgfsetfillopacity
它可以工作,只是不同的线条由于不透明度而混合:
我添加了一个pgftransparencygroup
,但它没有帮助。为确保万无一失,我检查了移除图案并使用正常填充。除非我使用该knockout=true
选项,否则它仍在混合。但这不会改变图案的结果。
减少版本的代码:
\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgflibrary{patterns}
\pgfdeclareshape{opacityTest}
{
\anchor{center}{\pgfpoint{0}{0}}
\backgroundpath{
\begin{pgftransparencygroup}[knockout=true]
\pgfsetlinewidth{2pt}
\pgfsetstrokeopacity{0.3}
\pgfsetfillopacity{0.3}
\pgfpathmoveto{\pgfpoint{0}{0}}
\pgfpathlineto{\pgfpoint{1cm}{0}}
\pgfusepath{stroke}
\pgfpathmoveto{\pgfpoint{0}{0}}
\pgfpathlineto{\pgfpoint{1cm}{0}}
\pgfpathlineto{\pgfpoint{1cm}{1cm}}
\pgfusepath{stroke}
\pgfpathmoveto{\pgfpoint{0}{0}}
\pgfpathlineto{\pgfpoint{1cm}{0}}
\pgfpathlineto{\pgfpoint{1cm}{1cm}}
\pgfpathlineto{\pgfpoint{0}{1cm}}
\pgfclosepath
%\pgfsetfillpattern{north west lines}{black}
\pgfusepath{fill}
\end{pgftransparencygroup}
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node[opacityTest] at (0,0) {};
\end{tikzpicture}
\end{document}
我在网上没有找到有关 pgf 这个问题的任何帮助,只找到了 tikz 的帮助。
提前致谢
Rmq:此代码仍将与 overleaf 融合。我主要在 Ubuntu 22.04 上使用 pdflatex(pdfTeX 3.141592653-2.6-1.40.22(TeX Live 2022/dev/Debian))
答案1
您可以在组外设置组透明度
\documentclass[tikz, border=1cm]{standalone}
\usepgflibrary{patterns}
\pgfdeclareshape{opacityTest}
{
\anchor{center}{\pgfpoint{0}{0}}
\backgroundpath{
\pgfsetlinewidth{2pt}
\pgfsetstrokeopacity{0.3}
\pgfsetfillopacity{0.3}
\begin{pgftransparencygroup}
\pgfpathmoveto{\pgfpoint{0}{0}}
\pgfpathlineto{\pgfpoint{1cm}{0}}
\pgfusepath{stroke}
\pgfpathmoveto{\pgfpoint{0}{0}}
\pgfpathlineto{\pgfpoint{1cm}{0}}
\pgfpathlineto{\pgfpoint{1cm}{1cm}}
\pgfusepath{stroke}
\pgfpathmoveto{\pgfpoint{0}{0}}
\pgfpathlineto{\pgfpoint{1cm}{0}}
\pgfpathlineto{\pgfpoint{1cm}{1cm}}
\pgfpathlineto{\pgfpoint{0}{1cm}}
\pgfclosepath
\pgfsetfillpattern{north west lines}{black}
\pgfusepath{fill}
\end{pgftransparencygroup}
}
}
\begin{document}
\begin{tikzpicture}
\node[opacityTest] at (0,0) {};
\end{tikzpicture}
\end{document}