当有子标题时,如何在列表中显示中心标题(列表标题和子标题类之间存在工作冲突)?

当有子标题时,如何在列表中显示中心标题(列表标题和子标题类之间存在工作冲突)?

我想使用列表包( \usepackage{listings}) 在论文中显示 matlab 代码(使用 elsarticle 类),但标题未在代码下方居中对齐。在我添加xleftmargin=3.5cmxrightmargin=3.5cm添加左右边距后发生了这种情况。如果我删除这两个选项,标题将出现在页面的中心!如何在列表中同时拥有边距和中心标题?

我用过\centering\begin{center}...\end{center}但是它们不起作用。

更新:我发现这\usepackage{subcaption}就是问题所在。没有它,标题就会居中。但我需要副标题!

我的代码:

\documentclass[3p,times]{elsarticle}

\usepackage{ecrc}
\volume{00}
\firstpage{1}
\journalname{Best Journal Ever}
\runauth{S. Gerami}
\jid{}
\jnltitlelogo{Best Journal Ever}
\CopyrightLine{2013}{Published by Elsevier Ltd.}
\usepackage{amssymb}
\usepackage[figuresright]{rotating}
\usepackage[ruled,vlined]{algorithm2e}

%%FOR MATH
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amsfonts}

%%I NEED THESE PACKAGED FOR FIGURES AND SUBCAPTIONs
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}

%%FOR CODES
\usepackage{listings}
\usepackage{color}

%%FOR TABLES
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\usepackage{lipsum}
\begin{document}

\begin{frontmatter}
\dochead{}
\title{title of my paper}

\author[bahonar]{Sajjad Gerami}
\ead{[email protected]}
\address[bahonar]{Faculty of Mathematics and Computer Science, Best University, Somewhere, The One Country}

\begin{abstract}
abstract text....
\end{abstract}

\end{frontmatter}

\section{First section}
\lipsum{}
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}

\lstset{ %
  backgroundcolor=\color{white},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
  basicstyle=\footnotesize,        % the size of the fonts that are used for the code
  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
  breaklines=true,                 % sets automatic line breaking
  captionpos=b,                    % sets the caption-position to bottom
  commentstyle=\color{mygreen},    % comment style
  deletekeywords={...},            % if you want to delete keywords from the given language
  escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
  extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  frame=single,                    % adds a frame around the code
  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  keywordstyle=\color{blue},       % keyword style
  language=Octave,                 % the language of the code
  morekeywords={*,...},            % if you want to add more keywords to the set
  numbers=left,                    % where to put the line-numbers; possible values are (none, left, right)
  numbersep=5pt,                   % how far the line-numbers are from the code
  numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
  rulecolor=\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
  showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  showstringspaces=false,          % underline spaces within strings only
  showtabs=false,                  % show tabs within strings adding particular underscores
  stepnumber=1,                    % the step between two line-numbers. If it's 1, each line will be numbered
  stringstyle=\color{mymauve},     % string literal style
  tabsize=2,                       % sets default tabsize to 2 spaces
%  title=Script 1: A program in Matlab to estimate the total area under sin(x),                   % show the filename of files included with \lstinputlisting; also try caption instead of title
  xleftmargin=3.5cm,
  xrightmargin=3.5cm
}

\begin{lstlisting}[title={Script 1: A program in Matlab to estimate the total area under $\sin(x)$}]
a=0;
b=pi/2;
h=max(feval(@func,linspace(a,b,n)));
x=a+(b-a)*rand(n,1);
y=h*rand(n,1);
N=0;
f=sin(x);
for j=1:n
    if y(j)<=f(j)
        N=N+1;
    end
end
ans=N/n*h*(b-a)
\end{lstlisting}
%*****************
\lipsum{}

\end{document}

输出:

在此处输入图片描述

更新2: 我测试了以下链接中提出的方法,但没有效果: 列表中央的标题

但后来我看到这个答案并编辑如下:

\begin{figure}[thp]
\begin{center}
  \begin{lstlisting}[numbers=left]
  My Code
  Another line
  \end{lstlisting}
\end{center}
\caption{my caption or title}
\end{figure}

结果: 在此处输入图片描述

很好,但是框和标题之间的差距太大了。我需要缩小它才能获得良好的效果。

更新3: 此链接对消除一幅图的间隙有最好的答案。

\begin{figure}[!ht]
  \centering
  \includegraphics[width=0.5\textwidth]{image}                   
  \vspace{-1em}
  \caption{\textcolor{gray}{\footnotesize \textit{ImageCaption}}}
  \vspace{-1.5em}
\end{figure}

答案1

实际上,当您使用和时caption,和之间存在一些不兼容性......listingsxleftmarginxrightmargin

在不更改任何来自某个包的命令的情况下,想到的一件事就是将列表插入居中的小页面内。

因此,删除以下行

  xleftmargin=3.5cm,
  xrightmargin=3.5cm

从内部\lstset

并以以下方式将您的列表插入到您的文档中:

\begin{center}
\begin{minipage}{.5\textwidth}
\begin{lstlisting}[title={Script 1: A program in Matlab to estimate the total area under $\sin(x)$}]
a=0;
b=pi/2;
h=max(feval(@func,linspace(a,b,n)));
x=a+(b-a)*rand(n,1);
y=h*rand(n,1);
N=0;
f=sin(x);
for j=1:n
    if y(j)<=f(j)
        N=N+1;
    end
end
ans=N/n*h*(b-a)
\end{lstlisting}
\end{minipage}
\end{center}

因此,以下 MWE

\documentclass[3p,times]{elsarticle}

%%I NEED THESE PACKAGED FOR FIGURES AND SUBCAPTIONs
\usepackage{graphicx}
%\usepackage{caption}  % no need because it is loaded by subcaption.
\usepackage{subcaption}

%%FOR CODES
\usepackage{listings}
\usepackage{color}

\usepackage{lipsum}
\begin{document}

\begin{frontmatter}
\title{title of my paper}

\author[bahonar]{Sajjad Gerami}

\begin{abstract}
abstract text....
\end{abstract}

\end{frontmatter}

\section{First section}
\lipsum{}
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}

\lstset{ %
  backgroundcolor=\color{white},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
  basicstyle=\footnotesize,        % the size of the fonts that are used for the code
  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
  breaklines=true,                 % sets automatic line breaking
  captionpos=b,                    % sets the caption-position to bottom
  commentstyle=\color{mygreen},    % comment style
  deletekeywords={...},            % if you want to delete keywords from the given language
  escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
  extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  frame=single,                    % adds a frame around the code
  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  keywordstyle=\color{blue},       % keyword style
  language=Octave,                 % the language of the code
  morekeywords={*,...},            % if you want to add more keywords to the set
  numbers=left,                    % where to put the line-numbers; possible values are (none, left, right)
  numbersep=5pt,                   % how far the line-numbers are from the code
  numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
  rulecolor=\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
  showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  showstringspaces=false,          % underline spaces within strings only
  showtabs=false,                  % show tabs within strings adding particular underscores
  stepnumber=1,                    % the step between two line-numbers. If it's 1, each line will be numbered
  stringstyle=\color{mymauve},     % string literal style
  tabsize=2,                       % sets default tabsize to 2 spaces
}
\begin{center}
\begin{minipage}{.5\textwidth}
\begin{lstlisting}[title={Script 1: A program in Matlab to estimate the total area under $\sin(x)$}]
a=0;
b=pi/2;
h=max(feval(@func,linspace(a,b,n)));
x=a+(b-a)*rand(n,1);
y=h*rand(n,1);
N=0;
f=sin(x);
for j=1:n
    if y(j)<=f(j)
        N=N+1;
    end
end
ans=N/n*h*(b-a)
\end{lstlisting}
\end{minipage}
\end{center}
\lipsum{}

\end{document} 

产量

在此处输入图片描述

只是一些评论:

  1. 由于我不知道从哪里获取该包ecrc,因此我从您的 MWE 中删除了一些代码。
  2. 该包caption不需要被加载,因为它已经被包加载了subcaption
  3. 我不知道您是否出于特定原因使用title而不是caption。如果原因是要删除标题中的“Listing”一词,您可以执行以下操作:

    a. 在序言中放入以下一行:

    \renewcommand{\lstlistingname}{Script}
    

    b. 替换

    \begin{lstlisting}[title={Script 1: A program in Matlab to estimate the total area under $\sin(x)$}]
    

    \begin{lstlisting}[caption={A program in Matlab to estimate the total area under $\sin(x)$}]
    

相关内容