The caption
package offers the possibility to define and execute own option sets via \captionsetup[<name>]{...}
and \captionsetup{options=<name>}
. This way you can define different looks for the captions of code
and tip
.
(Unfortunately the key options
is not documented well, currently it's only listed within the alphabetical reference of the caption
package documentation.)
To get tip
labeled and counted independently one needs to define an own counter for tip
. This part of the solution was taken from https://stackoverflow.com/questions/3900847/latex-listings-different-counters-for-different-listing-environments
\documentclass{article}
\usepackage{listings}
\usepackage{color}
\usepackage{caption}
\definecolor{listinggray}{gray}{0.98}
\definecolor{lbcolor}{rgb}{0.98,0.98,0.98}
\lstset{
% ...
float=tp,
% ...
}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[cmyk]{0.43, 0.35, 0.35,0.01}{\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1#2#3}}}
\captionsetup[code]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}
\lstnewenvironment{code}[1][]%
{\noindent\minipage{\linewidth}%
\lstset{#1}%
\captionsetup{options=code}% execute options set with \captionsetup[code]{...}
}
{\endminipage}
\makeatletter
\newcounter{tip}
\lstnewenvironment{tip}[1][]%
{%
\noindent\minipage{\linewidth}%
\def\lstlistingname{Tip}%
\def\lstlistingautorefname{Tip}%
\let\c@lstlisting=\c@tip
\let\thelstlisting=\thetip
\lstset{#1,keywordstyle=\color{black},numberbychapter=false,basicstyle=\footnotesize\ttfamily\color{black}\bfseries, backgroundcolor=\color{white},frame=none,captionpos=bc}%
\captionsetup{options=tip}% execute options set with \captionsetup[tip]{...}
}
{\endminipage}
\makeatother
\begin{document}
\begin{code}[caption=Test]
Code test..
\end{code}
....Look, just because I don't be givin' no man a foot massage don't make it right for Marsellus....
\begin{tip}[caption=Test]
A tip!
\end{tip}
\end{document}
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)$}]
Best Answer
If a listing has a
title
, it will be used instead ofcaption
, but without "Listing #" line.Unfortunately centered titles are hardcoded into listings, so you need to change the code. Here is a solution: I've just took the package defined
\@makecaption
and deleted the secong\hfil
and added\bfseries
(note that\lst@maketitle
uses\@makecaption
and drops the first part. If you want to keept captions centered but titles left-aligned, you need to patch\lst@maketitle
rather than\@maketitle
).Here us the result: