图像输出与预览不同的颜色 - HATCHCOLOR TIKZ

图像输出与预览不同的颜色 - HATCHCOLOR TIKZ

如下所示,当通过 Xetex/Xelatex 运行我的代码时,输​​出应该是一个带有阴影的红色矩形(第 1 页)和一个绿色圆圈(第 2 页)。

在此处输入图片描述

但是,当使用不同的程序(例如标准 Apple Viewer 或 Google Chrome 或 Adob​​e)打开 PDF 文件时,它显示黑色:

在此处输入图片描述

也许我的序言中的代码没有按照应有的方式定义,这会导致一些问题。有人能帮助我吗?

以下是代码:

\documentclass[11pt,oneside,german]{article} 
\usepackage{fontspec}  
\usepackage{pgf,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}}}

%--------links unten
\pgfdeclarepatternformonly[\hatchspread,\hatchthickness,\hatchcolor]{links unten}
{\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}
{\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}{\dimexpr    \hatchspread+2\hatchthickness}}
{\pgfqpoint{\dimexpr\hatchspread}{\dimexpr\hatchspread}}
{
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\hatchspread pt}{\hatchspread pt}}
    \pgfsetstrokecolor{\hatchcolor}
    \pgfusepath{stroke}
}

%--------rechts unten
\pgfdeclarepatternformonly[\hatchspread,\hatchthickness,\hatchcolor]{rechts unten}
{\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}
{\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}{\dimexpr    \hatchspread+2\hatchthickness}}
{\pgfqpoint{\dimexpr\hatchspread}{\dimexpr\hatchspread}}
{
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{\dimexpr\hatchspread+0pt}}
    \pgfpathlineto{\pgfqpoint{\dimexpr\hatchspread+0.15pt+0pt}{-0.15pt}}
    \pgfsetstrokecolor{\hatchcolor}
    \pgfusepath{stroke}
}
%----------------------------------------------------------------

\begin{document}

\begin{tikzpicture}[line cap=round,line join=round, x=1.0cm,y=1.0cm]
    \clip(2.22,0.86) rectangle (6.72,5.34);
    \draw[pattern=rechts unten,hatchspread=10pt,hatchthickness=0.5pt,hatchcolor= red](2.26,0.94) -- (6.7,0.94) -- (6.71,5.29) -- (2.26,5.3) -- cycle; 
\end{tikzpicture}

\newpage

\begin{tikzpicture}[line cap=round,line join=round, x=1.0cm,y=1.0cm] 
\draw[pattern=links unten,hatchspread=10pt,hatchthickness=0.5pt,hatchcolor= green] (0,0) circle (30mm); 
\end{tikzpicture}

\end{document}

答案1

PGF/TikZ 已经有适当的界面来设置仅形式图案的颜色:pattern color。这只是转发给\pgfsetfillpattern{<pattern>}{<color>}但是,它处理必要的颜色设置和混合等等。

因此,只需从模式声明中删除\hatchcolor和即可。为了兼容,我让它的参数转发给。\pgfsetstrokecolor{\hatchcolor}hatchcolorpattern color


我还删除了一些\dimexpr不必要的。

代码

% !TeX TS-program = xelatex
\documentclass[11pt, oneside, german]{article}
\usepackage{fontspec}
\usepackage{tikz}
\usetikzlibrary{patterns}
% defining the new dimensions and parameters
\newlength{\hatchspread}
\newlength{\hatchthickness}
\newlength{\hatchshift}
% declaring the keys in tikz
\tikzset{
  hatchspread/.code=\setlength{\hatchspread}{#1},
  hatchthickness/.code=\setlength{\hatchthickness}{#1},
  hatchshift/.code=\setlength{\hatchshift}{#1},
  hatchcolor/.style={pattern color={#1}}}
%--------links unten
\pgfdeclarepatternformonly[\hatchspread,\hatchthickness]{links unten}
  {\pgfqpoint{-2\hatchthickness}{-2\hatchthickness}}
  {\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}
             {\dimexpr\hatchspread+2\hatchthickness}}
  {\pgfqpoint{\hatchspread}{\hatchspread}}
  {%
    \pgfsetlinewidth{\hatchthickness}%
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}%
    \pgfpathlineto{\pgfqpoint{\hatchspread}{\hatchspread}}%
    \pgfusepath{stroke}%
  }
%--------rechts unten
\pgfdeclarepatternformonly[\hatchspread,\hatchthickness]{rechts unten}
  {\pgfqpoint{-2\hatchthickness}{-2\hatchthickness}}
  {\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}
             {\dimexpr\hatchspread+2\hatchthickness}}
  {\pgfqpoint{\hatchspread}{\hatchspread}}
  {%
    \pgfsetlinewidth{\hatchthickness}%
    \pgfpathmoveto{\pgfqpoint{0pt}{\hatchspread}}%
    \pgfpathlineto{\pgfqpoint{\dimexpr\hatchspread+0.15pt}{-0.15pt}}%
    \pgfusepath{stroke}%
  }
\begin{document}
\tikzset{line cap=round, line join=round, x=1.0cm, y=1.0cm}
\begin{tikzpicture}
\clip(2.22,0.86) rectangle (6.72,5.34);
\draw[
  pattern=rechts unten,
  hatchspread=10pt,
  hatchthickness=0.5pt,
  hatchcolor=red]
  (2.26,0.94) -- (6.7,0.94) -- (6.71,5.29) -- (2.26,5.3) -- cycle;
\end{tikzpicture}
\newpage
\tikz
\draw[
  pattern=links unten,
  hatchspread=10pt,
  hatchthickness=0.5pt,
  hatchcolor=green] (0,0) circle (30mm); 
\end{document}

相关内容