I need to insert picture in Latex book file, but picture must to be framed, and caption after picture also. That frame is not the same, picture and caption need horizontal line between. But picture need to be centered, and caption need to be aligned left in width of picture. Does anyone can help me?

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. It also helps a lot if you give us an idea of what you've tried since some people don't really like do this for me questions. Still, I'm sure someone will still help you. – JP-Ellis Feb 23 '16 at 13:14

One possible solution (maybe not the easiest...) is using the caption package to add a horizontal line above the caption and use the mdframed package to put a frame around the combination of figure and caption.

The horizontal line can be inserted with \DeclareCaptionFormat by putting it in front of the three arguments (figure number, separator, caption text) followed by a newline.

To make the line span the full frame, the margins have to be adjusted, which is possible with \mdfdefinestyle from mdframed.

Left alignment of the caption is possible with the justification option combined with singlelinecheck=false from in \captionsetup. However, this will justify to the figure environment and not to the width of the image as specified in \includegraphics. This is a bit more tricky, but a possible solution for this issue is to introduce a new length (in the MWE below \figwidth), set the value of this length to the preferred width of your image, and use the length both in \includegraphics and with a small calculation in the caption format to determine the indentation.




  \caption{Left aligned}
You can see an example of a black box in Figure \ref{fig:test}.

The demo option for graphicx is just to show the black box, this is not needed for actual application.


boxed caption example

This solution is adapted from Horizontal line below figure caption.


Here is a solution using \fbox instead of mdframed. All margins were set to \fboxsep. (Some of this was stolen from Marijn's solution.)

I should mention that \fboxsep and \fboxrule are adjustable.

\usepackage{showframe}% check centering


\newcommand{\pictureframe}[2]% #1=image, #2=caption
  \hspace{-\fboxsep}% extend to frame
  \rule{\dimexpr \textwidth+2\fboxsep}{\fboxrule}%

\pictureframe{\includegraphics[height=2in]{example-image}}{Left aligned}

framed picture

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.