Graphics, Figures & TablesHow to add a shadow under a picture ?

Information and discussion about graphics, figures & tables in LaTeX documents.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to add a shadow under a picture ?

Post by Cham »

How can I modify the following compilable code to add a gray drop shadow to the picture (including its frame) ?
I really don't understand the TikZ package. Is there a simple way to add the shadow ?

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\usepackage{here}
\definecolor{background}{RGB}{225,225,220}
\pagecolor{background}
\fboxsep=2mm

\begin{document}
bla bla bla
\medskip
\medskip
	\begin{figure}[H]
		\centering
		\fcolorbox{black}{white}{\includegraphics[height=8cm]{picture.jpg}}
		\caption{A caption.}
	\end{figure}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10328
Joined: Mon Mar 10, 2008 9:44 pm

How to add a shadow under a picture ?

Post by Stefan Kottwitz »

Hi Cham,

you could use \shadowbox of the fancybox package.

Since you mentioned TikZ, that's not hard. Add to your preamble

Code: Select all

\usepackage{tikz}
\usetikzlibrary{shadows}
and in your code you could use, for example, \node [drop shadow] :

Code: Select all

\begin{figure}[H]
\centering
  \tikz\node [drop shadow]
    {\fcolorbox{black}{white}{\includegraphics[height=8cm]{picture.png}}};
  \caption{A caption.}
\end{figure}
Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to add a shadow under a picture ?

Post by Cham »

Thanks ! It works ! 8-)

But how can I make the shadow fuzzy ?

Also, the drop shadow isn't properly placed. I tried to play with the shadow xshift and shadow yshift options, but it's not sufficient, apparently.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to add a shadow under a picture ?

Post by Cham »

Here's a compilable code. The drop shadow isn't right. How can I improve it ?

The shadow thickness is too strong. I don't know how to change it.
And is it possible to make the shadow fuzzy (blurry) ?

The shadow should be very subtle, with the same thickness at the right side and below the picture's frame. Currently, it's very hard to adjust the shadow. There must be a simpler way to do this.

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\usepackage{here}
\usepackage{tikz}
\usetikzlibrary{shadows}
\definecolor{background}{RGB}{225,225,220}
\pagecolor{background}
\fboxsep=2mm

\begin{document}
	\begin{figure}[H]
		\centering
		\tikz\node[drop shadow={
			top color=gray,
			bottom color=white,
			%fill=gray,
			opacity=0.2,
			shadow xshift=2pt,
			shadow yshift=-2pt
			}]
		{\fcolorbox{black}{white}{\includegraphics[height=8cm]{picture.jpg}}};
		\caption{A caption.}
	\end{figure}
\end{document}
EDIT : I'm trying to do this effect :
picture.jpg
picture.jpg (73.33 KiB) Viewed 19563 times
However, the shadow isn't right yet. The thickness should be exactly the same at the right and below the frame, with the same offset on the lower-left and upper-right corners. How ?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10328
Joined: Mon Mar 10, 2008 9:44 pm

How to add a shadow under a picture ?

Post by Stefan Kottwitz »

For making the shadow thinner, you could use the shadow scale option and possibly remove the x and y shifting, such as

Code: Select all

\tikz\node[drop shadow={
    shadow scale=0.98,
    top color=gray,
    opacity=0.5
  }]
  {\fcolorbox{black}{white}{\includegraphics[height=8cm]{picture.png}}};
Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: How to add a shadow under a picture ?

Post by Cham »

Well then, the offset isn't right. It's still slightly larger on the right side.

Take note that the picture isn't square. It's of rectangular shape, so I suspect it may be related to that.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to add a shadow under a picture ?

Post by Cham »

Is there any other way to drop a shadow under a picture, in LaTeX ? I think that the TikZ way is very cumbersome. Is there a way to add a shadow to a picture (including its frame, as defined in my example above), using another package, and that let you use a color/transparency as an option ?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10328
Joined: Mon Mar 10, 2008 9:44 pm

Re: How to add a shadow under a picture ?

Post by Stefan Kottwitz »

LaTeX is mainly for typesetting text. I guess you look for a graphics software. A graphics package such as TikZ would be an option though, which is capable of drawing even faded shadows, but now it's out of the question. Try Inkscape for example.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to add a shadow under a picture ?

Post by Cham »

I've found a good combination with fancybox, without TikZ. However, is there a way to add some transparency or color to the fancybox shadow ? Currently, the black lines are too heavy :

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\usepackage{here}
\usepackage{fancybox}
\definecolor{background}{RGB}{230,225,220}
\pagecolor{background}

\begin{document}
	\begin{figure}[H]
		\centering
		\shadowsize=1pt
		\fboxrule=0pt
		\fboxsep=0pt
		\shadowbox{\fboxsep=6pt\fcolorbox{white}{white}{\includegraphics[height=8cm]{Figures/picture.jpg}}}
		\caption{Another caption.}
	\end{figure}
\end{document}
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to add a shadow under a picture ?

Post by Cham »

Hmmm, maybe this is an hack ?

I've found a way to change the fancybox color, but I need to add another color directive since it's also changing the caption's text color :

Code: Select all

	\begin{figure}[H]
		\centering
		\shadowsize=1pt
		\fboxrule=0pt
		\fboxsep=0pt
		\color{gray}
		\shadowbox{\fboxsep=6pt\fcolorbox{white}{white}{\includegraphics[height=8cm]{picture.jpg}}}
		\color{black}
		\caption{Another caption.}
	\end{figure}
Is there a better way to achieve this ?

Here's a preview of the final effect, which I think is VERY nice !
shadow.jpg
shadow.jpg (99.72 KiB) Viewed 19548 times
Last edited by Cham on Mon Mar 12, 2012 10:40 pm, edited 1 time in total.
Post Reply