4

I'm trying to highlight an entire paragraph that contains a figure drawn with tikz. I'd like to highlight the text and also the blank space around the figure, while the figure itself should have the usual white background (the rectangle in my example means only that what's inside should not be shaded).

\documentclass{article}
\usepackage{tikz}
\begin{document}

Some text of the main body of the document.

\paragraph{Paragraph}
Some text of the paragraph.
\begin{figure}[h]
\centering
  \begin{tikzpicture}
     \node at (0,0) [rectangle,draw] { 
       \begin{tikzpicture}
       \draw [->] (-.5,0) -- (5,0) node [below] {$x$};
       \draw [->] (0,-.5) -- (0,3) node [left] {$y$};
       \end{tikzpicture}};
     \end{tikzpicture} 
  \caption{The rectangle only delimits the area without shaded background; it needn't be rendered in actual paragraph.}
  \label{fig:label}
\end{figure}

Some text of the paragraph. \\

Some text of the main body of the document.

\end{document}

I'm using this topic as a reference.

Is it even possible to do?

7

An example with mdframed. You will need the float specifier H from float package for your figure to avoid it floating.

I've encased your \paragraph in a newly defined colorpar environment and I've also given a white background to the tikzpicture.

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\usepackage[framemethod=TikZ]{mdframed}
\newmdenv[%
    innertopmargin=-\topskip,
    skipabove=.5\topskip,
    leftmargin=-10pt,
    rightmargin=-10pt,
    backgroundcolor=green!20,
    linewidth=0pt
]{colorpar}

\usepackage{float}

\begin{document}

Some text of the main body of the document.

\begin{colorpar}
\paragraph{Paragraph}
Some text of the paragraph.
\begin{figure}[H]
\centering
  \begin{tikzpicture}[tight background,
                      show background rectangle,
                      /tikz/background rectangle/.style={fill=white}
                     ]
     \node at (0,0) [rectangle,draw] {
       \begin{tikzpicture}
       \draw [->] (-.5,0) -- (5,0) node [below] {$x$};
       \draw [->] (0,-.5) -- (0,3) node [left] {$y$};
       \end{tikzpicture}};
     \end{tikzpicture}
  \caption{The rectangle only delimits the area without shaded background; it needn't be rendered in actual paragraph.}
  \label{fig:label}
\end{figure}

Some text of the paragraph.
\end{colorpar}

Some text of the main body of the document.

\end{document} 
|improve this answer|||||
  • Thank you, that worked out perfectly. I have just two questions: 1. Is this the usual way? Putting my figure in a node and shade the node over the background color? 2. Where can I find instructions on inserting images in posts like you did? – Pier Paolo Jan 7 '14 at 21:22
  • @PierPaolo I'm not a TikZ expert, I've learned the few things I know about it in this site. So I can't be of further help... – karlkoeller Jan 8 '14 at 8:34
5

By default, all TiKZ pictures are "transparent", what you call usual white background is not white, it looks like white because you draw them over a white background. Therefore when you include them over a colored background, this background color is shown through all not filled parts of the figure.

With backgrounds library help, it's easy to add a colored background to any TiKZ figure and you don't need to include it in any node nor surrounding figure, just add background definition options to tikzpicture environment.

Next example shows how to do it. It's similar to Karlkoeller solution but avoiding external figure and using tcolorbox instead of mdframed to created the colored paragraph. It also uses H float option for not floating figures.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usepackage{tcolorbox}
\usepackage{float}
\begin{document}

Some text of the main body of the document.

\begin{tcolorbox}[colback=blue!30]
\paragraph{Paragraph}
Some text of the paragraph.
\begin{figure}[H]
\centering
       \begin{tikzpicture}[background rectangle/.style={draw, fill=white},
                           show background rectangle]
       \draw [->] (-.5,0) -- (5,0) node [below] {$x$};
       \draw [->] (0,-.5) -- (0,3) node [left] {$y$};
       \fill[blue] (1,1) rectangle (2,2);
       \end{tikzpicture}
  \caption{The rectangle only delimits the area without shaded background; it needn't be rendered in actual paragraph.}
  \label{fig:label}
\end{figure}

Some text of the paragraph. \\
\end{tcolorbox}

Some text of the main body of the document.

\end{document}

enter image description here

|improve this answer|||||

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.