帶框圖片和帶框標題

帶框圖片和帶框標題

我需要在 Latex 書籍文件中插入圖片,但圖片必須加框,圖片後面還有標題。那幀不一樣,圖片和標題之間需要橫線。但圖片需要居中,標題需要在圖片寬度上左對齊。有人可以幫我嗎?

答案1

一個可能的解決方案(也許不是最簡單的......)是使用該caption包在標題上方添加一條水平線,並使用該mdframed包在圖形和標題的組合周圍放置一個框架。

\DeclareCaptionFormat可以透過將水平線放在三個參數(圖號、分隔符號、標題文字)前面並後面跟著換行符號來插入水平線。

為了使線條跨越整個框架,必須調整邊距,這可以透過\mdfdefinestylefrom進行調整mdframed

justification透過與singlelinecheck=falsefrom in組合的選項可以使標題左對齊\captionsetup。然而,這將證明圖形環境的合理性,而不是中指定的圖像的寬度\includegraphics。這有點棘手,但解決這個問題的一個可能的解決方案是引入一個新的長度(在下面的 MWE 中\figwidth),將該長度的值設定為圖像的首選寬度,然後\includegraphics在標題格式中進行小計算以確定縮排。

微量元素:

\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}

demo的選項只是graphicx顯示黑框,實際應用中不需要。

結果:

盒裝標題範例

該解決方案改編自圖片標題下方的水平線

答案2

這是一個使用\fbox而不是 的解決方案mdframed。所有邊距均設定為\fboxsep. (其中一些是從 Marijn 的解決方案中竊取的。)

我應該提到這一點\fboxsep並且\fboxrule可以調整。

\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}

帶框的圖片

相關內容