1

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?

  • It is certainly possible, e.g. by downgrading this code. – user121799 Mar 9 '18 at 21:58
2

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

|improve this answer|||||
  • Thank, but I would like to have the rectangle filled as well, which serves as background :) – Faceb Faceb Mar 9 '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. – user121799 Mar 9 '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 9 '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. – user121799 Mar 9 '18 at 22:41
  • Ok, but so far I am satisfied! :) Thanks again! – Faceb Faceb Mar 9 '18 at 22:48
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)

|improve this answer|||||
  • 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

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.