如何对齐小页面中不同子浮动元素内的标题?

如何对齐小页面中不同子浮动元素内的标题?

我在小页面中有多个子浮点数,如下所示。

\documentclass[10pt]{article}
\usepackage[margin=1.9cm]{geometry}

\usepackage[normalem]{ulem}
\usepackage{times}
\usepackage{adjustbox}
\usepackage{float}
\usepackage{caption}
\usepackage{listings}
\usepackage{subfig}
\usepackage{esvect}
\usepackage{xparse}


\captionsetup{justification=centering}

\lstset{
language=c++, 
keywordstyle=\color{black}\bfseries,
numbers=left, 
numberstyle=\scriptsize, 
numberblanklines=false,
stepnumber=1,
numbersep=5pt,
basicstyle=\ttfamily\scriptsize,
stringstyle=\ttfamily,
breaklines=true,
breakatwhitespace=true,
tabsize=2,
xleftmargin=1em,
showstringspaces=false,
}

\newsavebox{\boxedminipagebox}
\NewDocumentEnvironment{boxedminipage}{m O{\fboxsep}}
 {\par\kern#2\noindent\begin{lrbox}{\boxedminipagebox}
  \begin{minipage}{#1}\ignorespaces}
 {\end{minipage}\end{lrbox}%
  \makebox[#1]{%
    \kern\dimexpr-\fboxsep-\fboxrule\relax
    \fbox{\usebox{\boxedminipagebox}}%
    \kern\dimexpr-\fboxsep-\fboxrule\relax
  }\par\kern#2
 }

\begin{document}

\begin{figure*}
   \begin{minipage}{0.565\textwidth}
    {
    \begin{boxedminipage}{\textwidth}
      \subfloat[Small Code Caption]{
      %\begin{minipage}{\linewidth}
        \lstinputlisting[numbers=none]{small_code.txt}
        %\end{minipage}
      }
      \end{boxedminipage}

      \begin{boxedminipage}{\textwidth}
      \subfloat[Large Code Caption]{
       %\begin{minipage}{\linewidth}
        \lstinputlisting[numbers=none]{large_code.txt}
        %\end{minipage}
      }
       \end{boxedminipage}
    }
    \end{minipage}
    \hfill
    \begin{minipage}{0.41\textwidth}
    {
    \begin{boxedminipage}{\textwidth}
      \subfloat[Moderate Code Caption]{
        \lstinputlisting[numbers=none]{moderate_code.txt}
      }
       \end{boxedminipage}

    \begin{boxedminipage}{\textwidth}
      \subfloat[Another Small Code Caption]{
        \lstinputlisting[numbers=none]{small_code.txt}
      }
       \end{boxedminipage}
  }
    \end{minipage}
    %
  \caption{Weird Code Figure}
\end{figure*}

\end{document}

我得到以下输出: 在此处输入图片描述

我想将标题 (a) 和 (b) 以及标题 (c) 和 (d) 居中对齐。我尝试通过在子浮动内添加小页面来实现此目的(参见注释行),但这会进一步扰乱间距,因为 (b) 和 (c) 内的代码分成多行。

请注意,添加\centering之前\ignorespaces最终会移动所有我不想要的代码: 在此处输入图片描述

我怎样才能正确对齐标题​​,以便代码语句从左侧开始?

答案1

您必须使子浮动框的宽度与需要将标题置于中心的框一样宽。

\begin{filecontents*}{\jobname_small.txt}
system.out.println("Short text");
\end{filecontents*}
\begin{filecontents*}{\jobname_moderate.txt}
system.out.println("Some moderately long text");
\end{filecontents*}
\begin{filecontents*}{\jobname_large.txt}
system.out.println("Some very very very very very very long text");
\end{filecontents*}

\documentclass[10pt]{article}
\usepackage[margin=1.9cm]{geometry}

%\usepackage[normalem]{ulem}
\usepackage{times}
%\usepackage{adjustbox}
%\usepackage{float}
%\usepackage{caption}
\usepackage{listings}
\usepackage{subfig}
%\usepackage{esvect}
\usepackage{xparse}


\captionsetup{justification=centering}

\lstset{
  language=c++, 
  keywordstyle=\color{black}\bfseries,
  numbers=left, 
  numberstyle=\scriptsize, 
  numberblanklines=false,
  stepnumber=1,
  numbersep=5pt,
  basicstyle=\ttfamily\scriptsize,
  stringstyle=\ttfamily,
  breaklines=true,
  breakatwhitespace=true,
  tabsize=2,
  xleftmargin=1em,
  showstringspaces=false,
}

\newsavebox{\boxedminipagebox}
\NewDocumentEnvironment{boxedminipage}{m O{\fboxsep}}
 {\par\kern#2\noindent\begin{lrbox}{\boxedminipagebox}
  \begin{minipage}{#1}\ignorespaces}
 {\end{minipage}\end{lrbox}%
  \makebox[#1]{%
    \kern\dimexpr-\fboxsep-\fboxrule\relax
    \fbox{\usebox{\boxedminipagebox}}%
    \kern\dimexpr-\fboxsep-\fboxrule\relax
  }\par%\kern#2
 }

\begin{document}

\begin{figure*}

\begin{minipage}{0.565\textwidth}
  \begin{boxedminipage}{\textwidth}
  \subfloat[Small Code Caption]{%
    \makebox[\textwidth][l]{\lstinputlisting[numbers=none]{\jobname_small.txt}}%
  }
  \end{boxedminipage}

  \begin{boxedminipage}{\textwidth}
  \subfloat[Large Code Caption]{%
    \makebox[\textwidth][l]{\lstinputlisting[numbers=none]{\jobname_large.txt}}%
  }
  \end{boxedminipage}
\end{minipage}\hfill
\begin{minipage}{0.41\textwidth}
  \begin{boxedminipage}{\textwidth}
  \subfloat[Moderate Code Caption]{%
    \makebox[\textwidth][l]{\lstinputlisting[numbers=none]{\jobname_moderate.txt}}%
  }
  \end{boxedminipage}

  \begin{boxedminipage}{\textwidth}
  \subfloat[Another Small Code Caption]{%
    \makebox[\textwidth][l]{\lstinputlisting[numbers=none]{\jobname_small.txt}}%
  }
  \end{boxedminipage}
\end{minipage}

\caption{Weird Code Figure}

\end{figure*}

\end{document}

请小心处理可能产生空格的行尾。

在此处输入图片描述

相关内容