1

I have used the method of Ad Tikz filled rectangle that serves as background of block text in original document, with many thanks to @marmot, where gradient-color and rounded corners can be used.

However, when I add figures with white colors in it, the white color will be replaced by the colorbox, because of blend mode = multiply. How can I keep it white of any original color?

An example: enter image description here

The code is

\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}}
\usepackage{float}



\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}
    My original picture outside the box:
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.1]{tex_exchange.png}
    \end{figure}

\SimpleBox{My original picture inside the box:
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.1]{tex_exchange.png}
\end{figure}}

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

OK, if you really want to go that way, you may use

\documentclass{article}
\usepackage{eso-pic}
\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}}
\usepackage{float}



\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}
    My original picture outside the box:
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.1]{tex_exchange.png}
    \end{figure}

\SimpleBox{My original picture inside the box:
\begin{figure}[H]
    \centering
    \tikz[remember
    picture]{\node(pic){\includegraphics[scale=0.1]{tex_exchange.png};}}
    \AddToShipoutPictureFG*{\tikz[remember picture,overlay]{\node at (pic)
    {\includegraphics[scale=0.1]{tex_exchange.png}};}} 
\end{figure}}

\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

I loaded eso-pic and simply moved the pic to the foreground. If you want to do that several times, it might be more convenient to write a macro for this (I'll be happy to assist you in this task). Note that I had to rescale the picture since I only had a screen shot from your question.

  • i liked first sentence in your answer very much, therefore +1. your solution works, of course, but personally i prefer less demanding solution :-) – Zarko Mar 12 '18 at 14:47
2

using my solution to your previous question this work smoothly :-):

enter image description here

\documentclass{article}
\usepackage{graphicx}
% 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}
\ColorBox{blue!30}{\useparinfo
\lipsum[11]
    \begin{center}
\includegraphics{Capture}
    \end{center}
\lipsum[11]
}
\end{document}

addendum:

a simple solution which enable rounded corners as well gradient coloring for annotation boxes and including images without problem which you reported. all by help of tcolorbox:

\documentclass[a4paper]{article}
\usepackage[many]{tcolorbox}
\tcbset{oversize,
        enhanced,
        colframe=red!75!black,
        interior style={top color=blue!10,
                        bottom color=red!50}
        }
\usepackage{lipsum}

\begin{document}
\lipsum[11]
\begin{tcolorbox}
\lipsum[11]
\begin{center}
\includegraphics{Capture}
    \end{center}
\lipsum[11]
\end{tcolorbox}
\end{document}

enter image description here

off course you can named/defined own color boxes. this is special sensible, if you use them also for other purposes with different features.

  • How did you manage to use the same picture? :P – Faceb Faceb Mar 12 '18 at 12:40
  • thanks for your answer, but I would like to use gradient-color and rounded corners too – Faceb Faceb Mar 12 '18 at 12:41
  • what do you mean with 'and have problems with which you faced now'? – Faceb Faceb Mar 12 '18 at 13:37
  • @FacebFaceb, see addendum to my answer. is now as you like to have? – Zarko Mar 12 '18 at 14:09
  • @Zarko I also give you a +1 but why is your solution less demanding? Because you load a package that has much much more definitions in it than my proposal? The original question was if one could add boxes without changing the text, and I wrote a simple code that did that. Then there came several additional requirements that forced me to extend the code. – user121799 Mar 12 '18 at 14:56

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.