tikzpicture 的背景颜色

tikzpicture 的背景颜色

我从中学到这个答案在现有浮动元素后面放置背景颜色,而不必改变这些现有浮动元素的内容。

\documentclass{article}

\makeatletter

\def\foo#1\normalcolor\vbox\bgroup#2!!{%
\def\@xfloat ##1[##2]{#1%
 \normalcolor
      \hbox\bgroup{\color{yellow}\leaders\vrule\hskip\columnwidth\hskip-\columnwidth}%
      \vbox \bgroup\aftergroup\egroup
#2}}
\expandafter\foo\@xfloat{#1}[#2]!!

\makeatother

\usepackage{color}
\begin{document}

\begin{figure}
a\\b\\c
\caption{yes no}
\end{figure}

one two three

\end{document}

在此处输入图片描述

但是这里的方法对于tikzpicture绘制的图像不起作用,我想知道如何让tikzpicture实现同样的效果。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\usetikzlibrary{calc}

\begin{tikzpicture}
\coordinate (center) at (3,3);
\coordinate (1) at (0,0);
\coordinate (2) at (5, .5);
\coordinate (3) at ($(center) +(30:2)$);
\coordinate (4) at ($(center) +(70:2)$);
\coordinate (5) at (0,6);

\draw[blue, dotted]
      let \p1 =  ($(3)-(center)$),
          \n0 = {veclen(\x1,\y1)}
      in (center) circle(\n0);


\filldraw[draw=black, fill=green, fill opacity=0.3]
   let \p1 = ($(3) - (center)$),
       \p2 = ($(4) - (center)$),
       \n0 = {veclen(\x1,\y1)},            % Radius
       \n1 = {atan(\y1/\x1)+180*(\x1<0)},  % initial angle
       \n2 = {atan(\y2/\x2)+180*(\x2<0)}   % Final angle
    in
    (1) -- (2) --  (3) arc(\n1:\n2:\n0)  -- (5)  -- cycle;

\foreach \dot in {1,2,3,4,5,center} {
  \fill (\dot) circle(2pt);
  \node[above] at (\dot) {\dot};
}

\end{tikzpicture}
\end{document}

由于我有大量文件需要处理,我希望该方法更加自动化,而不需要我多次手动插入。期待您的答复!

答案1

它不起作用,因为tikzpicture它不是浮点数。如果你tikzpicture用浮点数包装你的\begin{figure}...\end{figure},并添加\makeatletter代码,它会像你之前的例子一样工作。

\documentclass{article}
\usepackage{tikz}
\makeatletter

\def\foo#1\normalcolor\vbox\bgroup#2!!{%
\def\@xfloat ##1[##2]{#1%
 \normalcolor
      \hbox\bgroup{\color{yellow}\leaders\vrule\hskip\columnwidth\hskip-\columnwidth}%
      \vbox \bgroup\aftergroup\egroup
#2}}
\expandafter\foo\@xfloat{#1}[#2]!!

\makeatother
\begin{document}
\thispagestyle{empty}
\usetikzlibrary{calc}

\begin{figure}
\centering
\begin{tikzpicture}
\coordinate (center) at (3,3);
\coordinate (1) at (0,0);
\coordinate (2) at (5, .5);
\coordinate (3) at ($(center) +(30:2)$);
\coordinate (4) at ($(center) +(70:2)$);
\coordinate (5) at (0,6);

\draw[blue, dotted]
      let \p1 =  ($(3)-(center)$),
          \n0 = {veclen(\x1,\y1)}
      in (center) circle(\n0);


\filldraw[draw=black, fill=green, fill opacity=0.3]
   let \p1 = ($(3) - (center)$),
       \p2 = ($(4) - (center)$),
       \n0 = {veclen(\x1,\y1)},            % Radius
       \n1 = {atan(\y1/\x1)+180*(\x1<0)},  % initial angle
       \n2 = {atan(\y2/\x2)+180*(\x2<0)}   % Final angle
    in
    (1) -- (2) --  (3) arc(\n1:\n2:\n0)  -- (5)  -- cycle;

\foreach \dot in {1,2,3,4,5,center} {
  \fill (\dot) circle(2pt);
  \node[above] at (\dot) {\dot};
}

\end{tikzpicture}
\caption{A tikzpicture inside a float}
\end{figure}
\end{document}

在此处输入图片描述

答案2

如果你想给 Ti 的背景上色例如,您可以backgrounds通过指定图片样式来使用 Z 图像库。[background rectangle/.style={fill=yellow}, show background rectangle]

\documentclass[tikz, border=7mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, backgrounds}

\begin{document}

  \begin{tikzpicture}[background rectangle/.style={fill=yellow}, show background rectangle]
    \coordinate (center) at (3,3);
    \coordinate (1) at (0,0);
    \coordinate (2) at (5, .5);
    \coordinate (3) at ($(center) +(30:2)$);
    \coordinate (4) at ($(center) +(70:2)$);
    \coordinate (5) at (0,6);

    \draw[blue, dotted]
        let \p1 =  ($(3)-(center)$),
            \n0 = {veclen(\x1,\y1)}
        in (center) circle(\n0);


    \filldraw[draw=black, fill=green, fill opacity=0.3]
       let \p1 = ($(3) - (center)$),
           \p2 = ($(4) - (center)$),
           \n0 = {veclen(\x1,\y1)},            % Radius
           \n1 = {atan(\y1/\x1)+180*(\x1<0)},  % initial angle
           \n2 = {atan(\y2/\x2)+180*(\x2<0)}   % Final angle
        in
        (1) -- (2) --  (3) arc(\n1:\n2:\n0)  -- (5)  -- cycle;

    \foreach \dot in {1,2,3,4,5,center} {
        \fill (\dot) circle(2pt);
        \node[above right] at (\dot) {\dot};
    }
  \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容