# Draw a line under a figure that match the figure lentgh

I am using a line under a figure to make it more aesthetic. I am having a problem with matching the line length with the figure length.

Here's a MWE.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\newcommand{\decoRule}{\rule{\textwidth}{.4pt}} % New command for a rule to be used under figures
\begin{document}

Example of how I define and use DecoRule.

\begin{figure}[tbp]
\centering
\includegraphics[scale=0.9]{cover}
\decoRule
\caption[Cover]{This is a cover.}
\end{figure}

\end{document}


You can use \widthof from the calc package:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}

\usepackage{calc}

\newcommand{\decoRule}[1]{\rule{#1}{.4pt}} % New command for a rule to be used under figures

\usepackage{mwe}

\begin{document}

Example of how I define and use DecoRule.

\begin{figure}[tbp]
\centering
\includegraphics[scale=0.7]{example-image-a}
\decoRule{\widthof{\includegraphics[scale=0.7]{example-image-a}}}
\caption[Cover]{This is a cover.}
\end{figure}

\end{document}


I added mwe so that I could compile without your specific figure.

It would probably be most efficient to make a newcommand that wraps the whole thing, instead of just making the decoline a command and having to retype the figure.

• You don't actually have to load the mwe package to use the images provided by it, as long as it's installed you're good to go with \includegraphics{example-image}. – Torbjørn T. Sep 29 '16 at 8:34

You need to couple the length of the rule with the figure width. Here it is done via \setbox but you can also do it with \settowidth

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
%\newcommand{\decoRule}{\rule{\textwidth}{.4pt}} % New command for a rule to be used under figures
\newcommand\decoImage[2][]{%
\setbox0\hbox{\includegraphics[#1]{#2}}
\parbox{\wd0}{%
\copy0
\hrulefill
}
}
\begin{document}
Text
\begin{figure}[tbp]
\centering
\decoImage[scale=0.4,angle=30]{example-image-a}
\caption[Cover]{This is a cover.}
\end{figure}
\end{document}