Effectively, there is some incopatibility between caption
and listings
when you use xleftmargin
and xrightmargin
...
Without changing any commands from one or the other package, one thing that comes to mind is to insert the listing inside a centered minipage.
So, remove the lines
xleftmargin=3.5cm,
xrightmargin=3.5cm
from inside the \lstset
and insert your listing in your document in the following way:
\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}
Thus, the following 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}
yields
Just some remarks:
- I've removed some code from your MWE since I didn't know where to get the package
ecrc
.
- the package
caption
doesn't need to be loaded since it is already loaded by the package subcaption
.
I don't know if you are using title
instead of caption
for a specific reason. If the reason is to get rid of the word "Listing" inside the caption you can do the following:
a. put the following line in the preamble:
\renewcommand{\lstlistingname}{Script}
b. replace
\begin{lstlisting}[title={Script 1: A program in Matlab to estimate the total area under $\sin(x)$}]
with
\begin{lstlisting}[caption={A program in Matlab to estimate the total area under $\sin(x)$}]
I would use a single figure
environment, start the adjustwidth
environment inside the figure
environment, and use two minipage
environments, side by side, to display the two images; each can be given its own \caption
. (The frame lines in the screenshot depict the ordinary page margins.)
\documentclass[letterpaper,11pt]{article}
\usepackage[demo]{graphicx} % omit 'demo' option in real doc
\usepackage[space]{grffile} % for filenames with spaces
\usepackage{float}
\usepackage{chngpage}
\usepackage{caption}
\usepackage{calc}
\usepackage[showframe]{geometry} % omit 'showframe' option in real doc
\begin{document}
\begin{figure}[ht!]
\begin{adjustwidth}{-1.25cm}{-1.25cm} % choose margin adjustments
\begin{minipage}{0.48\linewidth}
\caption{Schooling Time Shares}
\includegraphics[width=\linewidth]{Schooling Time Share over Time.png}
\end{minipage}
\hspace*{\fill} % spread out the two minipages
\begin{minipage}{0.48\linewidth}
\caption{Training Time Shares}
\includegraphics[width=\linewidth]{Training Time Share over Time.png}
\end{minipage}
\end{adjustwidth}
\end{figure}
\end{document}
Addendum: You mention in a comment that you weren't sure whether you should use subfigures. That decision depends mostly on how the two images are related to each other. If they're not particularly related, it makes sense to assign separate, figure-level captions to them. If, on the other hand, they're related quite closely, it's probably a good idea to treat them as subfigure
s. Fortunately, it's not hard to do so: (i) load the subcaption
package instead of (or in addition to) the caption
package, (ii) replace minipage
with subfigure
(four instances), and (iii) provide a new \caption
statement for the overall figure. The earlier captions, previously numbered 1 and 2, will now be labeled (a) and (b).
\documentclass[letterpaper,11pt]{article}
\usepackage[demo]{graphicx} % omit 'demo' option in real doc
\usepackage[space]{grffile} % for filenames with spaces
\usepackage{float}
\usepackage{chngpage}
\usepackage{subcaption}
\usepackage{calc}
\usepackage{geometry}
\begin{document}
\begin{figure}[ht!]
\caption{Two Types of Time Shares}
\begin{adjustwidth}{-1.25cm}{-1.25cm} % choose margin adjustments
\begin{subfigure}{0.48\linewidth}
\caption{Schooling Time Shares}
\includegraphics[width=\linewidth]{Schooling Time Share over Time.png}
\end{subfigure}
\hspace*{\fill} % spread out the two minipages
\begin{subfigure}{0.48\linewidth}
\caption{Training Time Shares}
\includegraphics[width=\linewidth]{Training Time Share over Time.png}
\end{subfigure}
\end{adjustwidth}
\end{figure}
\end{document}
Best Answer
You've not given us much go to on. Nevertheless I'm going to go out on a limb here and make some guesses about what might be going on.
According to your comments, the height of the two images is the same. In that case, there's nothing in your partial example that should result in misaligned images. Here's your partial example built into a MWE as follows
A possible issue might be that the contents of the caption might have different heights and depths. Here's a rather extreme example of what I'm referring to:
resulting in
Assuming nothing extreme is going on in the caption, you might consider adding
\strut
to the both caption lines. Of course, that wouldn't help in the extreme case I've demoed here.Essentially I'm suggesting you make sure the heights and depths of your two captions are equal.