0

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
1

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.

MWE:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{mdframed}
\mdfdefinestyle{boxcaption}{innerleftmargin=0cm,innerrightmargin=0cm}
\newlength{\figwidth}

\DeclareCaptionFormat{myformat}{\hrulefill\newline\hspace*{\dimexpr(\textwidth-\figwidth)/2}#1#2#3}
\captionsetup[figure]{format=myformat,justification=justified,singlelinecheck=false}

\begin{document}
\begin{figure}[tb]
\begin{mdframed}[style=boxcaption]
  \centering
  \setlength{\figwidth}{0.7\textwidth}
     \includegraphics[width=\figwidth]{./fig/test}
  \caption{Left aligned}
  \label{fig:test}
\end{mdframed}
\end{figure}
You can see an example of a black box in Figure \ref{fig:test}.
\end{document}

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

Result:

boxed caption example

This solution is adapted from Horizontal line below figure caption.

0

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.

\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}
%\usepackage{mwe}
\usepackage{showframe}% check centering

\captionsetup[figure]{justification=justified,singlelinecheck=false}%

\newcommand{\pictureframe}[2]% #1=image, #2=caption
{\sbox0{#1}%
\centering
\fbox{\begin{minipage}{\wd0}
  \baselineskip=0pt
  \abovecaptionskip=\fboxsep
  \belowcaptionskip=0pt
  \usebox0\par
  \vspace{\fboxsep}%
  \hspace{-\fboxsep}% extend to frame
  \rule{\dimexpr \textwidth+2\fboxsep}{\fboxrule}%
  \hspace{-\fboxsep}
  \caption{#2}
\end{minipage}}}

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

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.