对齐图表和表格标题

对齐图表和表格标题

我想将标题与图表的左侧对齐。之后,我想将标题的第一行与第二行对齐。我的 MWE 如下,您能帮我解决这个问题吗?

所需解决方案如下图所示。谢谢。

在此处输入图片描述 在此处输入图片描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tabularx}

\title{Title}

\begin{document}
\maketitle

\section{Introduction}

\begin{figure}[h!]
  \centering
  \includegraphics[height=7cm]{indir.jpg}
  \caption[Lorem ipsum dolor sit amet]{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed felis risus, ullamcorper ac suscipit ac, molestie non orci. Nam neque orci, facilisis in euismod sed, suscipit quis dolor.}
  \label{fig:heyelenturkiye}
\end{figure}


\begin{table}[!h]
\small
\centering
\caption[Lorem ipsum dolor sit amet]{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed felis risus, ullamcorper ac suscipit ac, molestie non orci. Nam neque orci, facilisis in euismod sed, suscipit quis dolor\\}
\begin{tabular}{cccc}
\hline
Row1    & Row2  & Row3  & Row4\\
\hline
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
\hline
\end{tabular}
\label{t1}
\end{table}

\end{document}

答案1

好吧,我定义了两个宏,一个用于图形(\weirdCapFigure),另一个用于表格(\weirdCapTable)。第一个宏接受图像作为其第一个参数,标题作为其第二个参数,第二个宏以相反的顺序工作,即标题优先。我还遵循了@leandriis 关于使用该包的建议caption

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tabularx}

\usepackage{caption} 
\captionsetup{format=hang}

\title{Title}

\begin{document}

\section{Introduction}

\newcommand{\weirdCapFigure}[2]{\sbox{0}{#1}#1
  \rule{\dimexpr.5\linewidth-0.5\wd0}{0pt}%
  \parbox[t]{\dimexpr.5\linewidth+0.5\wd0}{\caption{#2}}
}
\newcommand{\weirdCapTable}[2]{\sbox{0}{#2}
  \rule{\dimexpr.5\linewidth-0.5\wd0}{0pt}%
  \parbox[t]{\dimexpr.5\linewidth+0.5\wd0}{\caption{#1}}
  #2%
}

\begin{figure}[h!]
  \centering
  \weirdCapFigure{\includegraphics[height=7cm]{example-image}}
  {Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed felis risus, ullamcorper ac suscipit ac, molestie non orci. Nam neque orci, facilisis in euismod sed, suscipit quis dolor.\label{fig:heyelenturkiye}}  
\end{figure}


\begin{table}[!h]
\small
\centering
\weirdCapTable{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed felis risus, ullamcorper ac suscipit ac, molestie non orci. Nam neque orci, facilisis in euismod sed, suscipit quis dolor.\label{t1}}
{\begin{tabular}{cccc}
\hline
Row1    & Row2  & Row3  & Row4\\
\hline
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
\hline
\end{tabular}}
\end{table}

\end{document}

在此处输入图片描述

答案2

一种解决方法(但不是完整的解决方案)是删除\centering

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tabularx}

\usepackage{caption} 
\captionsetup{format=hang}
\title{Title}

\begin{document}
\maketitle

\section{Introduction}

\begin{figure}[h!]
%  \centering
  \includegraphics[height=7cm]{example-image-a}
  \caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed felis risus, ullamcorper ac suscipit ac, molestie non orci. Nam neque orci, facilisis in euismod sed, suscipit quis dolor.}
  \label{fig:heyelenturkiye}
\end{figure}


\begin{table}[!h]
\small
%\centering
\caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed felis risus, ullamcorper ac suscipit ac, molestie non orci. Nam neque orci, facilisis in euismod sed, suscipit quis dolor\\}
\begin{tabular}{cccc}
\hline
Row1    & Row2  & Row3  & Row4\\
\hline
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
\hline
\end{tabular}
\label{t1}
\end{table}

\end{document}

在此处输入图片描述 在此处输入图片描述

相关内容