lstinputlisting caption

1,586 views
Skip to first unread message

ritchie31

unread,
May 5, 2011, 12:53:42 AM5/5/11
to LaTeX Users Group
Hello forum, I wanted to change the caption of my figures (2) using
listing, for example:

\lstinputlisting[language=Matlab,caption=FEM-Weak
Formulation,label=list:fem]{heat2dFEM.m}
lstinputlisting[language=Matlab,caption=FDM-Explicit
scheme,label=list:explicit]{expheat2d.m}


but this appears in my document:


listing 1:FEM-Weak Formulation

listing 2:FDM-Explicit scheme


as titles of my figures, how can I change the listing1 and listing 2
to figure1 and figue 2 respectively?

Thanks in advance

Peter Flynn

unread,
May 5, 2011, 3:48:07 PM5/5/11
to latexus...@googlegroups.com
On Thu, May 5, 2011 at 5:53 AM, ritchie31 <medi...@gmail.com> wrote:
Hello forum, I wanted to change the caption of my figures (2) using
listing, for example:

\lstinputlisting[language=Matlab,caption=FEM-Weak
Formulation,label=list:fem]{heat2dFEM.m}
\lstinputlisting[language=Matlab,caption=FDM-Explicit
scheme,label=list:explicit]{expheat2d.m}

but this appears in my document:

listing 1:FEM-Weak Formulation
listing 2:FDM-Explicit scheme

That is correct. You have asked listings to create its own captions and labels, so they now belong to the class called "Listing". They are not Figures. They are Listings.

as titles of my figures, how can I change the listing1 and listing 2
to figure1 and figue 2 respectively?

Make them Figures instead:

\begin{figure}
\lstinputlisting[language=Matlab]{heat2dFEM.m}
\caption{FEM-Weak Formulation}
\label{list:fem}
\end{figure}

///Peter

ritchie31

unread,
May 5, 2011, 8:01:30 PM5/5/11
to LaTeX Users Group
Ok, but if I do that I have to scale my figures to fit in my document.
I just want to change "Listing 1" to "Figure 1" or "Code 1".
How to do it?

On May 5, 1:48 pm, Peter Flynn <anglebrac...@gmail.com> wrote:

Peter Flynn

unread,
May 6, 2011, 4:39:59 AM5/6/11
to latexus...@googlegroups.com
On Fri, May 6, 2011 at 1:01 AM, ritchie31 <medi...@gmail.com> wrote:
Ok, but if I do that I have to scale my figures to fit in my document.
I just want to change "Listing 1" to "Figure 1" or "Code 1".

It's not as simple as that: they are two different classes of objects.
 
How to do it?

I think you would need to reprogram a large amount of the listings package. This is probably best done by the package owner: why not ask their advice?

You could find the \lst@MakeCaption command in listings.sty and edit it to use the string "Figure", but then the counter for listings would interfere with (or be out of synchrony with) the counter for figures. You would need to mess around with the counters, and change the \addcontents lines to reference lof instead of lol, and probably a lot of other stuff as well.

Why would you need to scale the listing? If it's listing the same number of lines there shouldn't be very much difference.

///Peter
Reply all
Reply to author
Forward
0 new messages