[Tex/LaTex] Blurry Show Background

graphicstikz-pgf

I am trying to create a frame for each of my figures. Something's like this – a plain white rounded rectangle with a blurry shadow.

Shadow

Could this be done in latex? Could you show me how?

Best Answer

Here's a quick solution. It can be made more customizable, probably to incorporate pgfkeys for key-value options.

Code

\documentclass[parskip,a5paper]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main}

\newcommand{\myshadow}[5]% file name, scale, color, max color, decrease
{   \begin{tikzpicture}
        \node[rounded corners=3mm,inner sep=3mm,draw,fill=white] (temppicnode) {\includegraphics[scale=#2]{#1}};
        \begin{pgfonlayer}{background}
            \foreach \x in {1,...,10}
            {   \pgfmathsetmacro{\mycolor}{#4-#5*(\x-1)}
                \fill[rounded corners=3mm,#3!\mycolor] ($(temppicnode.south west)+(-0.5+\x/20,-0.5+\x/20)$) rectangle ($(temppicnode.north east)+(0.5-\x/20,0.5-\x/20)$);
            }
        \end{pgfonlayer}
    \end{tikzpicture}
}

\begin{document}
    \begin{figure}
        \myshadow{book.png}{1}{gray}{40}{4}
    \end{figure}
    \begin{figure}
        \myshadow{book.png}{1}{red}{100}{10}
    \end{figure}
        \begin{figure}
        \myshadow{book.png}{1}{blue}{50}{2}
    \end{figure}
\end{document}

Output

enter image description here


Edit 1: Oh, you wanted the inverse gradient. Here are both variants, with more things to tweak to your liking:

Code

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm,landscape]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\newcommand{\myborder}[8]% file name, scale, color, max color, decrease
{   \begin{tikzpicture}
        \node[rounded corners=#7 cm,inner sep=#7 cm,fill=white] (temppicnode) {\includegraphics[scale=#2]{#1}};
        \begin{pgfonlayer}{background}
            \foreach \x [evaluate=\x] in {1,...,#8}
            {   \pgfmathsetmacro{\mycolor}{#4 -(#4-#5)/#8*(\x-1)}
                \pgfmathsetmacro{\mycorners}{#6 +#7 -\x*#6/#8}
                \fill[rounded corners=\mycorners cm,#3!\mycolor] ($(temppicnode.south west)+(-#6+\x*#6/#8,-#6+\x*#6/#8)$) rectangle ($(temppicnode.north east)+(#6-\x*#6/#8,#6-\x*#6/#8)$);
            }
        \end{pgfonlayer}
    \end{tikzpicture}
}

\newcommand{\myshadow}[8]% file name, scale, color, max color, min color, width, inner radius, steps
{   \begin{tikzpicture}
        \node[rounded corners=#7 cm, inner sep=#7 cm, fill=white] (temppicnode) {\includegraphics[scale=#2]{#1}};
        \begin{pgfonlayer}{background}
            \foreach \x [evaluate=\x] in {1,...,#8}
            {   \pgfmathsetmacro{\mycolor}{#5+(#4-#5)/#8*(\x-1)}
                \pgfmathsetmacro{\mycorners}{#6+ #7 -\x*#6/#8}
                \fill[rounded corners=\mycorners cm,#3!\mycolor] ($(temppicnode.south west)+(-#6+\x*#6/#8,-#6+\x*#6/#8)$) rectangle ($(temppicnode.north east)+(#6-\x*#6/#8,#6-\x*#6/#8)$);
            }
        \end{pgfonlayer}
    \end{tikzpicture}
}

\begin{document}
    \myshadow{book.png}{1}{gray}{80}{5}{0.5}{0.3}{10}
    \myshadow{book.png}{1}{gray}{80}{5}{0.5}{0.3}{50}
    \myborder{book.png}{1}{gray}{80}{5}{0.5}{0.3}{10}
    \myborder{book.png}{1}{gray}{80}{5}{0.5}{0.3}{50}

    \myshadow{book.png}{1}{red}{40}{5}{0.3}{0.1}{10}
    \myshadow{book.png}{1}{red}{40}{5}{0.3}{0.1}{50}
    \myborder{book.png}{1}{blue}{20}{2}{0.3}{0.1}{10}
    \myborder{book.png}{1}{blue}{20}{2}{0.3}{0.1}{50}
\end{document}

Output

enter image description here


Edit 2: Fixed a problem with the border width computation:

Code

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm,landscape]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\newcommand{\myborder}[8]% file name, scale, color, max color, decrease
{   \begin{tikzpicture}
        \node[rounded corners=#7 cm,inner sep=#7 cm,fill=white] (temppicnode) {\includegraphics[scale=#2]{#1}};
        \begin{pgfonlayer}{background}
            \foreach \x [evaluate=\x] in {1,...,#8}
            {   \pgfmathsetmacro{\mycolor}{#4 -(#4-#5)/#8*(\x-1)}
                \pgfmathsetmacro{\mycorners}{#6 +#7 -(\x-1)*#6/(#8-1)}
                \fill[rounded corners=\mycorners cm,#3!\mycolor] ($(temppicnode.south west)+(-\mycorners+#7,-\mycorners+#7)$) rectangle ($(temppicnode.north east)+(\mycorners-#7,\mycorners-#7)$);
            }
        \end{pgfonlayer}
    \end{tikzpicture}
}

\newcommand{\myshadow}[8]% file name, scale, color, max color, min color, width, inner radius, steps
{   \begin{tikzpicture}
        \node[rounded corners=#7 cm, inner sep=#7 cm, fill=white] (temppicnode) {\includegraphics[scale=#2]{#1}};
        \begin{pgfonlayer}{background}
            \foreach \x [evaluate=\x] in {1,...,#8}
            {   \pgfmathsetmacro{\mycolor}{#5+(#4-#5)/#8*(\x-1)}
                \pgfmathsetmacro{\mycorners}{#6+ #7 -(\x-1)*#6/(#8-1)}
                                \fill[rounded corners=\mycorners cm,#3!\mycolor] ($(temppicnode.south west)+(-\mycorners+#7,-\mycorners+#7)$) rectangle ($(temppicnode.north east)+(\mycorners-#7,\mycorners-#7)$);
            }
        \end{pgfonlayer}
    \end{tikzpicture}
}

\begin{document}
    \myshadow{book.png}{1}{gray}{80}{5}{0.5}{0.3}{10}
    \myshadow{book.png}{1}{gray}{80}{5}{0.5}{0.3}{50}
    \myborder{book.png}{1}{gray}{50}{2}{0.5}{0.3}{10}
    \myborder{book.png}{1}{gray}{50}{2}{0.5}{0.3}{50}

    \myshadow{book.png}{1}{red}{40}{5}{0.3}{0.1}{10}
    \myshadow{book.png}{1}{red}{40}{5}{0.3}{0.1}{50}
    \myborder{book.png}{1}{blue}{20}{2}{0.3}{0.1}{10}
    \myborder{book.png}{1}{blue}{20}{2}{0.3}{0.1}{50}
\end{document}

Output

enter image description here