标题和文本宽度的奇怪交互

标题和文本宽度的奇怪交互

我正在尝试为一些照片创建一些好看的标题。我不想调整这些照片的纵横比,所以我keepaspectratio在 中包含了该选项\includegraphics

受到 Martin Scharrer 的回答的启发浮动的自动宽度,我使用一个保存框来捕获照片的宽度,然后在命令中使用此宽度\captionsetup。这种方法效果很好,但我注意到一个相当奇怪的现象:

  • 如果将宽度选项\includegraphics设置为width=1\textwidth,则所有内容都会按应有的方式编译。
  • 如果将宽度选项\includegraphics设置为width=a\textwidth,其中 a 小于 1,则标题将向右偏移约 2pt,因此无法在图像下方正确居中。
  • 如果将宽度选项\includegraphics设置为width=b\textwidth,其中 b 大于 1,则标题和图像之间会有少量空白(尽管在这种情况下标题不会偏移)。

以下是 MWE:

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{tgbonum}
\usepackage{tikz}
\usepackage{caption}
\usepackage{float}
\usepackage{tcolorbox}
\usepackage{calc}

%include own graphics path \graphicspath{{/Users/...}}


%%captions formatting%%
\definecolor{captionColor}{RGB}{103,143,150}
\DeclareCaptionFormat{customCaption}{%
  \begin{tcolorbox}[
    nobeforeafter,
    colback=captionColor,
    arc=0pt,
    outer arc=0pt,
    boxrule=0pt,
    colupper=white,
    %fontupper=\normal,
    boxsep=0pt
  ]
  #1#2#3
  \end{tcolorbox}%
}


\begin{document}



The beginning of the document stretches the full text width.

\begin{minipage}[t]{0.60\textwidth}\vspace{0pt}
There will be some text on the left side of the page.  The start of this text needs to be vertically aligned with the top of the other minipage.
\end{minipage}
\quad \quad \quad
\begin{minipage}[t]{0.29\textwidth}\vspace{0pt}
%FIRST PHOTO
%use savebox to get the width of photo1
\newsavebox\placeHolderOne
\sbox\placeHolderOne{\includegraphics[width=.9\linewidth,height=.9\linewidth,keepaspectratio]{examplePhoto1}}
%set the width of the caption to the width of photo1
\captionsetup{type=figure,width=\wd\placeHolderOne,format=customCaption,labelformat=empty,justification=raggedright,position=bottom,skip=0pt,textfont={it}}
%include photo1, with caption
\includegraphics[width=.9\linewidth,height=.9\linewidth,keepaspectratio]{examplePhoto1}
{\caption[]{Caption is offset to the side when width < linewidth}}
\vspace{6mm}
%SECOND PHOTO
%use savebox to get the width of photo2
\newsavebox\placeHolderTwo
\sbox\placeHolderTwo{\includegraphics[width=1.2\linewidth,height=.9\linewidth,keepaspectratio]{examplePhoto2}}
%set the width of the caption to the width of photo2
\captionsetup{type=figure,width=\wd\placeHolderTwo,format=customCaption,labelformat=empty,justification=raggedright,position=bottom,skip=0pt,textfont={it}}
%include photo2, with caption
\includegraphics[width=1.2\linewidth,height=.9\linewidth,keepaspectratio]{examplePhoto2}
{\caption[]{A white space appears between image and caption when width>linewidth}}
\end{minipage}
\end{document}

我的输出截图

答案1

另一种方法是图形及其标题都由自定义排版\tcbox。注意tcolorbox不会使用caption包完成的标题设置,因此您必须自己动手。

\documentclass{article}
\usepackage[T1]{fontenc}

\usepackage{tgbonum}
\usepackage{tcolorbox}
\tcbuselibrary{skins}

\definecolor{captionColor}{RGB}{103,143,150}

\newtcbox[use counter*=figure, list inside=lof]\tcbfigure[1]{
  enhanced,
  nobeforeafter,
  colback=captionColor,
  colbacktitle=captionColor,
  arc=0pt,
  outer arc=0pt,
  boxrule=0pt,
  boxsep=0pt,
  leftupper=0pt, rightupper=0pt,
  top=0pt, bottom=0pt,
  toptitle=3mm,
  bottomtitle=3mm,
  flip title,
  fonttitle=\itshape,
  % before title=\RaggedRight, % need ragged2e package
  #1
}

\begin{document}
The beginning of the document stretches the full text width.

\begin{minipage}[t]{0.60\textwidth}\vspace{0pt}
  There will be some text on the left side of the page.  The start of this text needs to be vertically aligned with the top of the other minipage.
\end{minipage}
%
\quad \quad \quad
%
\begin{minipage}[t]{0.29\textwidth}\vspace{0pt}
  \tcbfigure{title=Caption is offset to the side when width < linewidth}
    {\includegraphics[width=.9\linewidth]{example-image}}\par
  \vspace{6mm}

  \tcbfigure{title=A white space appears between image and caption when width > linewidth}
    {\includegraphics[width=1.2\linewidth]{example-image}}
\end{minipage}
\end{document}

在此处输入图片描述

答案2

这个答案仅有的解决了第一个问题,

当宽度 < 线宽时,标题向一侧偏移

caption选项width=<length>不仅设置了字幕的宽度,还可以设置字幕的居中位置,您看到的就是居中效果。

要控制标题宽度和左边距,请使用margin={0pt, \dimexpr\linewidth-<desired width>}

演示居中对齐的示例width=<length>

\documentclass{article}
\usepackage{caption}
\usepackage[pass, showframe]{geometry}
\usepackage{graphicx}
\usepackage[T1]{fontenc}


\begin{document}
  \captionsetup{type=figure, width=3cm}
  \includegraphics[width=3cm]{example-image}
  \caption{|text\hfill text|}  
  
  \captionsetup{type=figure, width=6cm}
  \includegraphics[width=6cm]{example-image}
  \caption{|text\hfill text|}
\end{document}

在此处输入图片描述

您的示例中,使用margin={0pt,\dimexpr\linewidth-\wd\placeHolder(One|Two)}而不是width=\wd\placeHolder(One|Two),文档类,以及而不是 的article图像:example-image-[ab]examplePhoto[12]

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{tgbonum}
\usepackage{tikz}
\usepackage{caption}
\usepackage{float}
\usepackage{tcolorbox}
\usepackage{calc}

%include own graphics path \graphicspath{{/Users/...}}


%%captions formatting%%
\definecolor{captionColor}{RGB}{103,143,150}
\DeclareCaptionFormat{customCaption}{%
  \begin{tcolorbox}[
    nobeforeafter,
    colback=captionColor,
    arc=0pt,
    outer arc=0pt,
    boxrule=0pt,
    colupper=white,
    %fontupper=\normal,
    boxsep=0pt
  ]
  #1#2#3
  \end{tcolorbox}%
}


\begin{document}



The beginning of the document stretches the full text width.

\begin{minipage}[t]{0.60\textwidth}\vspace{0pt}
There will be some text on the left side of the page.  The start of this text needs to be vertically aligned with the top of the other minipage.
\end{minipage}
\quad \quad \quad
\begin{minipage}[t]{0.29\textwidth}\vspace{0pt}
%FIRST PHOTO
%use savebox to get the width of photo1
\newsavebox\placeHolderOne
\sbox\placeHolderOne{\includegraphics[width=.9\linewidth,height=.9\linewidth,keepaspectratio]{example-image-a}}
%set the width of the caption to the width of photo1
\captionsetup{type=figure,margin={0pt,\dimexpr\linewidth-\wd\placeHolderOne},format=customCaption,labelformat=empty,justification=raggedright,position=bottom,skip=0pt,textfont={it}}
%include photo1, with caption
\includegraphics[width=.9\linewidth,height=.9\linewidth,keepaspectratio]{example-image-a}
{\caption[]{Caption is offset to the side when width < linewidth}}
\vspace{6mm}
%SECOND PHOTO
%use savebox to get the width of photo2
\newsavebox\placeHolderTwo
\sbox\placeHolderTwo{\includegraphics[width=1.2\linewidth,height=.9\linewidth,keepaspectratio]{example-image-b}}
%set the width of the caption to the width of photo2
\captionsetup{type=figure,margin={0pt,\dimexpr\linewidth-\wd\placeHolderTwo},format=customCaption,labelformat=empty,justification=raggedright,position=bottom,skip=0pt,textfont={it}}
%include photo2, with caption
\includegraphics[width=1.2\linewidth,height=.9\linewidth,keepaspectratio]{example-image-b}
{\caption[]{A white space appears between image and caption when width>linewidth}}
\end{minipage}
\end{document}

在此处输入图片描述

相关内容