是否可以使用 xcolor 包为图片的某个区域着色?

是否可以使用 xcolor 包为图片的某个区域着色?

我想为我在环境中创建的图片区域添加阴影\picture。我可以通过将其设置在坐标之间来为该区域添加阴影吗?例如,我可以用蓝色为(1,1), (1,4), (3,1), (3,4)等之间的区域添加阴影吗?

答案1

您的意思是彩色的矩形框,类似于下面的那种?

\documentclass{article}
\usepackage{xcolor}

\begin{document}
\setlength{\unitlength}{1cm}
\fbox{%
  \begin{picture}(5,5)(0,0)
    \put(1,1){\textcolor{blue}{\rule{2\unitlength}{3\unitlength}}}
  \end{picture}%
}
\end{document}

结果

答案2

在我看来——从本页上的所有讨论来看——人们似乎更喜欢 tikZ。我对这个包不太熟悉,所以我会让别人给出一个 tikZ 解决方案。

但是,如果您正在使用picture环境,那么重写代码pstricks就不会那么难了。

如果你正在使用pstricks,你可以创建一个自定义环境

\pscustom[linewidth=0.2pt,fillstyle=solid,fillcolor=blue]{
    \pspolygon(1,1)(1,4)(3,1)(3,4)
}

这是一个更完整的例子

\documentclass{article}
\usepackage{pstricks}
\psset{unit=0.5cm}
\begin{document}

Based upon the order of your coordinates \vspace{\baselineskip}

\begin{pspicture}(4,4)
    \pscustom[linewidth=0.2pt,fillstyle=solid,fillcolor=blue]{
        \pspolygon(1,1)(1,4)(3,1)(3,4)
    }
\end{pspicture}

But perhaps you meant, \vspace{\baselineskip}

\begin{pspicture}(4,4)
    \pscustom[linewidth=2pt,fillstyle=solid,fillcolor=gray]{
        \pspolygon(1,1)(1,4)(3,4)(3,1)
    }
\end{pspicture}


\end{document}

在此处输入图片描述

答案3

无论如何,这里有一个tikz与环境一起使用的解决方案picture

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\tikzstyle{every picture}+=[remember picture]
\setlength{\unitlength}{0.5cm}

\begin{document}
If you just want a rectangle, \vspace{\baselineskip}

\begin{picture}(4,4)(0,0)
    \put(1,1){\tikz \coordinate (botleft);}
    \put(3,4){\tikz \coordinate (topright);}
    \put(0,0){\tikz[overlay] \fill[blue] (botleft) rectangle (topright);}
\end{picture}

Based upon the order of your coordinates \vspace{\baselineskip}

\begin{picture}(4,4)(0,0)
    \put(1,1){\tikz \coordinate (point1);}
    \put(1,4){\tikz \coordinate (point2);}
    \put(3,1){\tikz \coordinate (point3);}
    \put(3,4){\tikz \coordinate (point4);}
    \put(0,0){\tikz[overlay] \fill[blue] (point1) -- (point2)%
             -- (point3) -- (point4) -- cycle;}
\end{picture}

But perhaps you meant, \vspace{\baselineskip}

\begin{picture}(4,4)(0,0)
    \put(1,1){\tikz \coordinate (point5);}
    \put(1,4){\tikz \coordinate (point6);}
    \put(3,1){\tikz \coordinate (point8);}
    \put(3,4){\tikz \coordinate (point7);}
    \put(0,0){\tikz[overlay] \fill[blue] (point5) -- (point6)%
             -- (point7) -- (point8) -- cycle;}
\end{picture}


\end{document}

您可能需要运行两次才能得到正确的结果。以下是运行后的结果:

在此处输入图片描述

相关内容