0

Is it possible to put a Tikz filled rectangle behind a block of text that serves as background color? Although the text is inside the Tikz node, the margins and positions of the text block should remain unaltered, i.e. the rectangle should adapt to text positions/margins.

Example of original document enter image description here

After adding the background color enter image description here

Is it maybe possible?

Source: How to make my own LaTeX template?

2 Answers2

1

Something like this? EDIT: Fill, blend mode and made it possible that the box runs over a page break.

\documentclass{article}
\usepackage{tikz}
\usepackage{refcount}
\usetikzlibrary{backgrounds,calc}
\usepackage{tikzpagenodes}
\usepackage{lipsum}
\tikzset{SimpleBox/.style={draw,red,thick,fill=blue,fill opacity=0.3,rounded
corners}}



\newcounter{tmp}

\newcommand{\StartBox}[1][]{%
  \stepcounter{tmp}%from https://tex.stackexchange.com/a/52579/121799
  \tikz[remember picture,overlay, baseline=(Begin.base)]{%
  \node[anchor=base,inner sep=0pt,outer sep=0pt] (Begin) {\strut};}
  \label{a\thetmp}%
  \ifnum\getpagerefnumber{a\thetmp}=\getpagerefnumber{b\thetmp} \else
  \begin{tikzpicture}[overlay, remember picture]
   \path (current page text area.north west) -- (current page text area.south west)
    node(WestLine)[left]{};
   \path (current page text area.north east) -- (current page text area.south east)
    node(EastLine)[right]{};
   \begin{scope}[on background layer,blend mode=multiply]
   \draw[SimpleBox,#1] ($(current page text area.south-|WestLine)-(0,2pt)$)
   --      (Begin.north-|WestLine) -- (Begin.north-|EastLine) 
   --  ($(current page text area.south-|EastLine)-(0,2pt)$);
   \end{scope}
  \end{tikzpicture}%
  \fi%
}

\newcommand{\EndBox}[1][]{%
\tikz[remember picture,overlay, baseline=(End.base)]{%
\node[anchor=base,inner sep=0pt,outer sep=0pt] (End) {\strut};}%
  \label{b\thetmp}
  \ifnum\getpagerefnumber{a\thetmp}=\getpagerefnumber{b\thetmp}
  \begin{tikzpicture}[overlay,remember picture]
   \path (current page text area.north west) -- (current page text area.south west)
    node(WestLine)[left]{};
   \path (current page text area.north east) -- (current page text area.south east)
    node(EastLine)[right]{};
   \begin{scope}[on background layer,blend mode=multiply]
    \draw[SimpleBox,#1] (Begin.north-|WestLine) rectangle (End.south-|EastLine);
   \end{scope}
  \end{tikzpicture}
  \else
  \begin{tikzpicture}[overlay,remember picture]
   \path (current page text area.north west) -- (current page text area.south west)
    node(WestLine)[left]{};
   \path (current page text area.north east) -- (current page text area.south east)
    node(EastLine)[right]{};
   \begin{scope}[on background layer,blend mode=multiply]
   \draw[SimpleBox,#1] ($(current page text area.north-|WestLine)+(0,2pt)$)
   --      (End.south-|WestLine) -- (End.south-|EastLine) 
   --  ($(current page text area.north-|EastLine)-(0,2pt)$);
   \end{scope}
  \end{tikzpicture}
  \fi
}

\newcommand{\SimpleBox}[2][]{%
\StartBox[#1]%
#2\EndBox[#1]}


\begin{document}

\SimpleBox{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a leo ut
erat luctus scelerisque. Suspendisse auctor mauris sit amet fringilla
hendrerit. Maecenas eu tortor eu diam fringilla scelerisque sit amet eget enim.
Nam vitae bibendum erat, sit amet scelerisque felis. Suspendisse placerat
vitae velit pharetra lobortis et marmottae. Donec ut erat a erat porta aliquet. Nulla eget
augue sem. Proin orci neque, eleifend id eleifend id, venenatis sit amet diam.
Mauris lacus est, cursus eget sapien at, pharetra lobortis ante. Proin
tempus ipsum tellus, ut ultricies magna congue nec. Vestibulum ante ipsum primis
in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse potenti.
In posuere dapibus urna, vitae euismod magna iaculis at. Pellentesque pretium,
lorem ut vehicula egestas, velit dolor porta nunc, id interdum ipsum purus vel
dolor.}

\lipsum[2]

\tikzset{SimpleBox/.style={draw,blue,thick,fill=red,fill opacity=0.3}}

\SimpleBox{\lipsum[3]}

\lipsum[4]


\SimpleBox[draw=yellow,fill=yellow]{Wanna yellow?}


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a leo ut
erat luctus scelerisque. Suspendisse auctor mauris sit amet fringilla
hendrerit. Maecenas eu tortor eu diam fringilla scelerisque sit amet eget enim.
Nam vitae bibendum erat, sit amet scelerisque felis. Suspendisse placerat
vitae velit pharetra lobortis et marmottae. Donec ut erat a erat porta aliquet. Nulla eget
augue sem.

\SimpleBox{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a leo ut
erat luctus scelerisque. Suspendisse auctor mauris sit amet fringilla
hendrerit. Maecenas eu tortor eu diam fringilla scelerisque sit amet eget enim.
Nam vitae bibendum erat, sit amet scelerisque felis. Suspendisse placerat
vitae velit pharetra lobortis et marmottae. Donec ut erat a erat porta aliquet. Nulla eget
augue sem. Proin orci neque, eleifend id eleifend id, venenatis sit amet diam.
Mauris lacus est, cursus eget sapien at, pharetra lobortis ante. Proin
tempus ipsum tellus, ut ultricies magna congue nec. Vestibulum ante ipsum primis
in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse potenti.
In posuere dapibus urna, vitae euismod magna iaculis at. Pellentesque pretium,
lorem ut vehicula egestas, velit dolor porta nunc, id interdum ipsum purus vel
dolor.}
\end{document}

enter image description here

  • Thank, but I would like to have the rectangle filled as well, which serves as background :) – Faceb Faceb Mar 09 '18 at 22:30
  • @FacebFaceb No problem. Note that I left the lower box in its present form on purpose just to warn you that lipsum adds a \par automatically. –  Mar 09 '18 at 22:37
  • Thank you very much! Can I do all usual stuff like using the align environment etc inside these rectangles? – Faceb Faceb Mar 09 '18 at 22:39
  • @FacebFaceb Sure (if you add \usepackage{amsmath} of course). Note, however, that the present version does not work with pagebreaks. It is possible to cook up a version that does work with page breaks, but this is much more effort, and I'd kindly ask you to ask a separate question if you want this. –  Mar 09 '18 at 22:41
  • Ok, but so far I am satisfied! :) Thanks again! – Faceb Faceb Mar 09 '18 at 22:48
  • Is the text slightly blue and red, because the rectangle is placed over the text? – Faceb Faceb Mar 10 '18 at 21:18
  • @FacebFaceb If you worry about this, change \begin{scope}[on background layer] to \begin{scope}[blend mode=multiply]. –  Mar 10 '18 at 21:25
  • Do you have tips how to give color as an extra argument to the \SimpleBox command, for example \SimpleBox{red}{\lipsum[2]} and \SimpleBox{blue}{\lipsum[2]} – Faceb Faceb Mar 11 '18 at 07:13
  • Thanks! :) Another question: if the color argument is given in \SimpleBox[]{}, then what is the color definition \tikzset{SimpleBox/.style={}} doing? – Faceb Faceb Mar 11 '18 at 12:08
  • @FacebFaceb I thought you wanted the short cut. \tikzset{SimpleBox/.style={...}} is the cleaner way of doing this. the optional argument just appends the styles. That is, if you want to have an overall style, use \tikzset{SimpleBox/.style={...}} , if you just want to add a style in one box, use [...]. –  Mar 11 '18 at 13:03
  • Suppose I would like to add a figure that has white colors in it. Right now the white color is replace by the boxcolor, because of 'blend mode = multiply'. How can I keep it white or any original color? – Faceb Faceb Mar 11 '18 at 22:07
  • @FacebFaceb The probably simplest way is to put the figure on a different layer (the box is on the background layer) and/or use the blend mode to switch to another blend mode. (But does that look good if the figure has still a white background? I personally also color the figure in such situations.) –  Mar 11 '18 at 22:11
  • Could you provide an example in your answer with an example-figure? – Faceb Faceb Mar 11 '18 at 22:18
  • Hello, I have to run now. This is also starting to become a bit cumbersome. According to the policy of this forum you were supposed to ask a new question. If you add a figure to your example I will try to look at it in a few days. –  Mar 11 '18 at 22:23
1

for annotate some paragraphs with background color or with framed background color you not need to use tikz hackary ... use of color boxes provided by tcolorbox can gives the same result:

enter image description here

mwe:

\documentclass{article}
% for indentation of a text in mini page
\parskip=1ex    %  just drop if you not like it
\newcommand{\saveparinfos}{%
\edef\myindent{\the\parindent}%
\edef\myparskip{\the\parskip}}

\newcommand{\useparinfo}{%
\setlength{\parindent}{\myindent}%
\setlength{\parskip}{\myparskip}}
\saveparinfos
% color boxes
\usepackage{xcolor}
\newcommand\ColorBox[2]%
{\noindent\hspace*{-\dimexpr2\fboxsep+2\fboxrule\relax}\mbox{
\fcolorbox{#1}{#1}{%
\begin{minipage}{\linewidth}%
#2
\end{minipage}}}\par
}
\newcommand\ColorFBox[2]%
{\noindent\hspace*{-\dimexpr2\fboxsep+2\fboxrule\relax}
 \setlength\fboxrule{1pt}
\fcolorbox{#1}{#1!30}{%
\begin{minipage}{\linewidth}
#2
\end{minipage}}\par
}

% dummy text
\usepackage{lipsum}

\begin{document}
\lipsum[1]
\ColorBox{blue!30}{\useparinfo % in case when you like to have 
                                % in minipage the same indentation
                                % as it is main text
\lipsum[11]\lipsum[12]
                 }
\lipsum[11]
\ColorFBox{red}{\lipsum[12]}
\end{document}

note: content of boxes (as well as tikz in nodes) can not be broken to two pages. for this you should consider tcolorbox package)

Zarko
  • 296,517
  • is it possible to use gradient color and round corners? – Faceb Faceb Mar 11 '18 at 12:35
  • this demand is not in your question* . with this simple colorbox not possible, but with with use of tcolorbox yes. advantages of my answer is simplicity and one pass compiling.
  • – Zarko Mar 11 '18 at 12:57