将标题与居中图形的左侧对齐

将标题与居中图形的左侧对齐

虽然搜索这个主题得到了一些结果,但没有一个对我有用。我需要定义一个命令,将图像水平居中放置在页面上,但其标题与图像左侧对齐。

我尝试过minipage一些\captionsetup{justification=raggedright}没有用的方法,目前我尝试使用\captionbox同样没有用的方法。

\documentclass[a4paper,10pt,notitlepage]{article}

\usepackage{graphicx} 
\usepackage{caption}
\usepackage{subcaption}
\usepackage{float}
\usepackage{xcolor}

% CAPTION STYLE
\DeclareCaptionFormat{labelstyle}{
    \setlength\fboxsep{2pt}
    \colorbox{blue}{{#1}}#2#3
} 
\definecolor{blue}{HTML}{669933}

\newcommand{\midpict}[4]{    
        \begin{figure}[H]
            \centering
                \captionsetup{format=labelstyle, justification=raggedright}
                \captionbox{#3\label{#4}}{\includegraphics[width=#2\textwidth]{#1}}
        \end{figure}
}

\begin{document}
    \midpict{a.png}{0.5}{Caption}{fig:A}
\end{document}

答案1

您必须添加singlelinecheck=false到字幕设置中。

\documentclass[a4paper,10pt,notitlepage]{article}

\usepackage{graphicx} 
\usepackage{caption}
\usepackage{float}
\usepackage{xcolor}

% CAPTION STYLE
\DeclareCaptionFormat{labelstyle}{% <- needed
    \setlength\fboxsep{2pt}% <- needed
    \colorbox{blue}{{#1}}#2#3%
} 
\definecolor{blue}{HTML}{669933}

\newcommand{\midpict}[4]{%
  \begin{figure}[H]
    \centering
      \setcaptionwidth{#2\textwidth}
      \captionsetup{format=labelstyle,
        %justification=raggedright,% <- only needed if longer captions should not be justified
        singlelinecheck=false% <- added
      }
      \includegraphics[width=#2\textwidth]{#1}
      \caption{#3}\label{#4}
  \end{figure}%
}

\begin{document}
\midpict{example-image}{0.5}{Caption}{fig:A}
\end{document}

在此处输入图片描述


关于以下评论:您也可以使用 aminipage来代替\setcaptionwidth

\documentclass[a4paper,10pt,notitlepage]{article}

\usepackage{graphicx} 
\usepackage{caption}
\usepackage{float}
\usepackage{xcolor}

% CAPTION STYLE
\DeclareCaptionFormat{labelstyle}{% <- needed
    \setlength\fboxsep{2pt}% <- needed
    \colorbox{blue}{{#1}}#2#3%
} 
\definecolor{blue}{HTML}{669933}

\newcommand{\midpict}[4]{%
  \begin{figure}[H]
    \centering
      \captionsetup{format=labelstyle,
        %justification=raggedright,% <- only needed if longer captions should not be justified
        singlelinecheck=false% <- added
      }
      \begin{minipage}{#2\textwidth}
        \includegraphics[width=\linewidth]{#1}
      \caption{#3}\label{#4}
      \end{minipage}
  \end{figure}%
}

\begin{document}
\midpict{example-image}{0.5}{Caption}{fig:A}
\end{document}

也许您不需要包float

\documentclass[a4paper,10pt,notitlepage]{article}

\usepackage{graphicx} 
\usepackage{caption}
\usepackage{xcolor}

% CAPTION STYLE
\DeclareCaptionFormat{labelstyle}{% <- needed
    \setlength\fboxsep{2pt}% <- needed
    \colorbox{blue}{{#1}}#2#3%
} 
\definecolor{blue}{HTML}{669933}

\newcommand{\midpict}[4]{%
  \begin{center}
    \begin{minipage}{#2\textwidth}
        \captionsetup{format=labelstyle,
          %justification=raggedright,% <- only needed if longer captions should not be justified
          singlelinecheck=false% <- added
        }
        \includegraphics[width=\linewidth]{#1}
      \captionof{figure}{#3}\label{#4}
      \end{minipage}
  \end{center}%
}

\begin{document}
\midpict{example-image}{0.5}{Caption}{fig:A}
\end{document}

相关内容