打印时 Tikz 图案区域变黑

打印时 Tikz 图案区域变黑

恐怕这个问题以前已经问过,但我找不到。

当我在 tikzpicture 环境中有一个带有阴影填充的元素时,打印时填充会变成黑色。

例子:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}

\begin{tikzpicture}
\draw[pattern=north west lines, pattern color=black] (0,0) rectangle (2,4);
\end{tikzpicture}

\end{document}

我更愿意将 tikzpicture 保留在我的文档中,而不是复制并使用 \includegraphics 包含它。有人能告诉我我做错了什么吗?或者如果这是一个普遍问题,有解决方案吗?

提前致谢。

答案1

打印时矩形背景似乎变黑,使用黑色阴影线后,整个矩形都变黑了。我通过使用灰线避免了这个问题,并按照 eiterorm 所示定义了自己的图案。

示例如下:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

%\usetikzlibrary{backgrounds}

\usepackage{tikz}
\usetikzlibrary{patterns}

% defining the new dimensions and parameters
\newlength{\hatchspread}
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}
% declaring the keys in tikz
\tikzset{hatchspread/.code={\setlength{\hatchspread}{#1}},
         hatchthickness/.code={\setlength{\hatchthickness}{#1}},
         hatchshift/.code={\setlength{\hatchshift}{#1}},% must be >= 0
         hatchcolor/.code={\renewcommand{\hatchcolor}{#1}}}
% setting the default values
\tikzset{hatchspread=3pt,
         hatchthickness=0.4pt,
         hatchshift=0pt,% must be >= 0
         hatchcolor=black}
% declaring the pattern
\pgfdeclarepatternformonly[\hatchspread,\hatchthickness,\hatchshift,\hatchcolor]% variables
   {custom north west lines}% name
   {\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}% lower left corner
   {\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}{\dimexpr\hatchspread+2\hatchthickness}}% upper right corner
   {\pgfqpoint{\dimexpr\hatchspread}{\dimexpr\hatchspread}}% tile size
   {% shape description
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{\dimexpr\hatchspread+\hatchshift}}
    \pgfpathlineto{\pgfqpoint{\dimexpr\hatchspread+0.15pt+\hatchshift}{-0.15pt}}
    \ifdim \hatchshift > 0pt
      \pgfpathmoveto{\pgfqpoint{0pt}{\hatchshift}}
      \pgfpathlineto{\pgfqpoint{\dimexpr0.15pt+\hatchshift}{-0.15pt}}
    \fi
    \pgfsetstrokecolor{\hatchcolor}
%    \pgfsetdash{{1pt}{1pt}}{0pt}% dashing cannot work correctly in all situation this way
    \pgfusepath{stroke}
   }

\begin{document}

\begin{center}
%
\begin{tikzpicture}
\draw[pattern=custom north west lines] (0,0) rectangle (3,4);
\end{tikzpicture}%
%
\begin{tikzpicture}
\draw[pattern=custom north west lines,hatchspread=5pt,hatchthickness=2pt,hatchcolor=gray!20,] (0,0) rectangle (3,4);
\end{tikzpicture}%
%
\begin{tikzpicture}
\draw[pattern=custom north west lines,hatchspread=8pt,hatchthickness=5pt, hatchcolor=gray!20] (0,0) rectangle (3,4);
\end{tikzpicture}%
%
\begin{tikzpicture}
\draw[pattern=custom north west lines,hatchspread=10pt,hatchthickness=3pt, hatchcolor=gray!20] (0,0) rectangle (3,4);
\end{tikzpicture}%

\end{center}

\end{document}

不过,通过改变实际的背景颜色可能会有更优雅的解决方案。

相关内容