3

I learned from this answer to put a background color behind existing floats without having to change the content of these existing floats.

\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}

enter image description here

But the method here doesn't work for images drawn by tikzpicture, and I wonder how to achieve the same effect for 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}

Since I have a large number of files to work with, I would like the method to be more automated and not require me to manually insert many times. Looking forward to your answer!

happy
  • 149
  • 7
  • 2
    It doesn't work because tikzpicture is not a float. If you wrap your tikzpicture in a \begin{figure}...\end{figure} float, and add the \makeatletter code, it works as your prior example. – Steven B. Segletes Jan 02 '22 at 15:51

2 Answers2

2

It doesn't work because tikzpicture is not a float. If you wrap your tikzpicture in a \begin{figure}...\end{figure} float, and add the \makeatletter code, it works as your prior example.

\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}

enter image description here

2

If you want to color the background of a TikZ image you can use backgrounds library by specifying [background rectangle/.style={fill=yellow}, show background rectangle] as picture style, for example.

\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&lt;0)},  % initial angle
       \n2 = {atan(\y2/\x2)+180*(\x2&lt;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}

enter image description here

Kpym
  • 23,002