5

As shown on the picture below, the TikZ backgruound library allows to create the background with automatical width/height settings.

enter image description here

\usetikzlibrary{backgrounds}
\begin{tikzpicture}
[show background rectangle, background rectangle/.style={fill=magenta}]
\shade[ball color=blue] (0,0) circle (10ex);
\end{tikzpicture}

What if we want to define width or assign \textwidth, \columnwidth etc. as width of the background layer? The sketch of the desired result represented on the picture below.

enter image description here

4

You may define a new key named auto centering. This key adds an empty node fitted around the content of your tikzpicture. The minimum width of this node is the length given as argument (default value is \columnwidth):

\usetikzlibrary{backgrounds,fit}
\makeatletter
\tikzset{
  auto centering/.style={execute at end picture={
      \node[fit=(current bounding box),minimum width=#1-2*\tikz@framexsep,inner sep=0,
      ]{};
    }},
  auto centering/.default=\columnwidth,
}
\makeatother

In your document, add this key as first option of your tikzpictures.

Here is an example:

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,fit}
\makeatletter
\tikzset{
  auto centering/.style={execute at end picture={
      \node[fit=(current bounding box),minimum width=#1-2*\tikz@framexsep,inner sep=0,
      ]{};
    }},
  auto centering/.default=\columnwidth,
}
\makeatother

\usepackage{multicol}
\usepackage{lipsum}
\begin{document}
\lipsum[4]

\noindent\begin{tikzpicture}
[auto centering,show background rectangle,background rectangle/.style={fill=magenta}]
\shade[ball color=blue] (0,0) circle (10ex);
\end{tikzpicture}

\begin{multicols}{2}
\lipsum[4]

\noindent\begin{tikzpicture}
[auto centering,show background rectangle, background rectangle/.style={fill=orange}]
\shade[ball color=blue] (0,0) circle (10ex);
\end{tikzpicture}

\noindent\begin{tikzpicture}
[auto centering,show background rectangle, background rectangle/.style={fill=lime}]
\shade[ball color=blue] (0,0) circle (10ex);
\end{tikzpicture}

\lipsum[4]

\end{multicols}
\end{document}
2

First draw the sphere in the middle:

\shade[ball color=blue] (0.5\linewidth,0) circle (10ex);   %% note 0.5\linewidth

To increase the width of the background to \textwidth, as you are painting the background rectangle, it is enough to draw some thing that runs across the text width like

\path(0,0) --(\linewidth,0);

This is an invisible line that runs across the \linewidth. Add inner frame xsep=0pt additionally to background rectangle.

\documentclass{article}
\usepackage{tikz,showframe}   %% showframe just for demo
\usetikzlibrary{backgrounds}
\begin{document}
\noindent
\begin{tikzpicture}%
[show background rectangle,inner frame xsep=0pt, background rectangle/.style={fill=magenta,line width=0pt}]     
%% you can also use `tight background` instead of `inner frame xsep`.
\shade[ball color=blue] (0.5\linewidth,0) circle (10ex);
\path(0,0) --(\linewidth,0);
\end{tikzpicture}%
\end{document}

enter image description here

Here is a simpler version in which the background is drawn first as a rectangle.

\documentclass{article}
\usepackage{tikz,showframe}
\begin{document}
\noindent
\begin{tikzpicture}%
\path[use as bounding box,line width=0pt,fill=magenta](-0.5\linewidth,-11ex) rectangle(0.5\linewidth,11ex);
\shade[ball color=blue] (0,0) circle (10ex);
\end{tikzpicture}%
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.