Discussion:
listings captions
(too old to reply)
Lars Madsen
2011-07-21 12:09:52 UTC
Permalink
Hi does anyone have an idea as to how one can get listings to work with
empty captions?

As it is now if one use

\begin{lstlisting}[caption={}]
test
\end{lstlisting}

then no caption will be printed

We would like to be able to get a header/caption in the manner of

Listing 1:

or if say the caption package is used

Listing 1

(i.e. no colon)
--
/daleif (remove RTFSIGNATURE from email address)

Memoir and mh bundle maintainer
LaTeX FAQ: http://www.tex.ac.uk/faq
LaTeX book: http://www.imf.au.dk/system/latex/bog/ (in Danish)
Remember to post minimal examples, see URL below
http://www.minimalbeispiel.de/mini-en.html
Lars Madsen
2011-07-21 12:20:06 UTC
Permalink
Post by Lars Madsen
Hi does anyone have an idea as to how one can get listings to work with
empty captions?
As it is now if one use
\begin{lstlisting}[caption={}]
test
\end{lstlisting}
then no caption will be printed
We would like to be able to get a header/caption in the manner of
or if say the caption package is used
Listing 1
(i.e. no colon)
to make things simpler we make use of

\lstnewenvironment

so perhaps some local redefinitions may help
--
/daleif (remove RTFSIGNATURE from email address)

Memoir and mh bundle maintainer
LaTeX FAQ: http://www.tex.ac.uk/faq
LaTeX book: http://www.imf.au.dk/system/latex/bog/ (in Danish)
Remember to post minimal examples, see URL below
http://www.minimalbeispiel.de/mini-en.html
GL
2011-07-21 12:23:36 UTC
Permalink
Post by Lars Madsen
Post by Lars Madsen
Hi does anyone have an idea as to how one can get listings to work
with empty captions?
As it is now if one use
\begin{lstlisting}[caption={}]
test
\end{lstlisting}
then no caption will be printed
We would like to be able to get a header/caption in the manner of
or if say the caption package is used
Listing 1
(i.e. no colon)
to make things simpler we make use of
\lstnewenvironment
so perhaps some local redefinitions may help
listings basically test, as expected,

\ifx \***@caption\@empty do nothing \else print the caption\fi

So:

\begin{lstlisting}[caption={\hskip1sp}]

can handle this case ;-)
GL
2011-07-21 12:24:49 UTC
Permalink
Post by GL
Post by Lars Madsen
to make things simpler we make use of
\lstnewenvironment
so perhaps some local redefinitions may help
listings basically test, as expected,
\begin{lstlisting}[caption={\hskip1sp}]
can handle this case ;-)
Or may be with more sense: caption=\null
Lars Madsen
2011-07-21 12:31:11 UTC
Permalink
Post by GL
Post by GL
Post by Lars Madsen
to make things simpler we make use of
\lstnewenvironment
so perhaps some local redefinitions may help
listings basically test, as expected,
\begin{lstlisting}[caption={\hskip1sp}]
can handle this case ;-)
Or may be with more sense: caption=\null
but this leaves the : which we wanted to be removed when the caption is
empty

caption={\hskip1sp}

will also give 'Listing 1:' if the caption package is used
--
/daleif (remove RTFSIGNATURE from email address)

Memoir and mh bundle maintainer
LaTeX FAQ: http://www.tex.ac.uk/faq
LaTeX book: http://www.imf.au.dk/system/latex/bog/ (in Danish)
Remember to post minimal examples, see URL below
http://www.minimalbeispiel.de/mini-en.html
GL
2011-07-21 12:53:13 UTC
Permalink
Post by Lars Madsen
Post by GL
Post by GL
Post by Lars Madsen
to make things simpler we make use of
\lstnewenvironment
so perhaps some local redefinitions may help
listings basically test, as expected,
\begin{lstlisting}[caption={\hskip1sp}]
can handle this case ;-)
Or may be with more sense: caption=\null
but this leaves the : which we wanted to be removed when the caption is
empty
caption={\hskip1sp}
will also give 'Listing 1:' if the caption package is used
Ah ? The caption is typeset with \***@makecaption, a pointer
to \@makecaption that may be redefined as for example:

\long\def\***@makecaption#1#2{\def\@tempa {\null }%
\vskip\abovecaptionskip
\sbox\@tempboxa{#1\ifx \***@caption\@tempa \else : #2\fi }
\ifdim \wd\@tempboxa >\hsize
#1\ifx \***@caption\@tempa \else : #2\fi \par
\else
\global \@minipagefalse
\***@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}%

\begin{lstlisting}[caption=\null]
coucou
\end{lstlisting}
GL
2011-07-21 12:56:31 UTC
Permalink
Post by GL
Post by Lars Madsen
Post by GL
Post by GL
Post by Lars Madsen
to make things simpler we make use of
\lstnewenvironment
so perhaps some local redefinitions may help
listings basically test, as expected,
\begin{lstlisting}[caption={\hskip1sp}]
can handle this case ;-)
Or may be with more sense: caption=\null
but this leaves the : which we wanted to be removed when the caption is
empty
caption={\hskip1sp}
will also give 'Listing 1:' if the caption package is used
\vskip\abovecaptionskip
\else
\fi
\vskip\belowcaptionskip}%
\begin{lstlisting}[caption=\null]
coucou
\end{lstlisting}
Or you can replace the \null sentinel by another test:

\long\def\***@makecaption#1#2{\setbox0=\hbox{\***@caption}\ifdim
\wd0=\z@ \@tempswafalse \else \@tempswatrue \fi
\vskip\abovecaptionskip
\sbox\@tempboxa{#1\***@tempswa : #2\fi }
\ifdim \wd\@tempboxa >\hsize
#1\***@tempswa : #2\fi \par
\else
\global \@minipagefalse
\***@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}%

So that any caption which is not \@empty but whose width is null
produces the desired result (\kern0pt, \unskip, \null etc.)
Lars Madsen
2011-07-21 13:03:18 UTC
Permalink
Post by GL
Post by GL
Post by Lars Madsen
Post by GL
Post by GL
Post by Lars Madsen
to make things simpler we make use of
\lstnewenvironment
so perhaps some local redefinitions may help
listings basically test, as expected,
\begin{lstlisting}[caption={\hskip1sp}]
can handle this case ;-)
Or may be with more sense: caption=\null
but this leaves the : which we wanted to be removed when the caption is
empty
caption={\hskip1sp}
will also give 'Listing 1:' if the caption package is used
\vskip\abovecaptionskip
\else
\fi
\vskip\belowcaptionskip}%
\begin{lstlisting}[caption=\null]
coucou
\end{lstlisting}
\vskip\abovecaptionskip
\else
\fi
\vskip\belowcaptionskip}%
produces the desired result (\kern0pt, \unskip, \null etc.)
doesnt' that defy the purpose of using the caption package to control
the font etc. of the caption?

I think it would be better to redefine \***@MakeCaption

The only problem is those tests against \@empty

Thus changing \***@MakeCaption, and making the caption key tell us when
it is being used, may help.

come to think of it it might even be useful (for my application) to have
more control of what is written to a 'list of' file, in which case I
will need to mess with \***@MakeCaption anyway.
--
/daleif (remove RTFSIGNATURE from email address)

Memoir and mh bundle maintainer
LaTeX FAQ: http://www.tex.ac.uk/faq
LaTeX book: http://www.imf.au.dk/system/latex/bog/ (in Danish)
Remember to post minimal examples, see URL below
http://www.minimalbeispiel.de/mini-en.html
GL
2011-07-21 13:19:17 UTC
Permalink
Post by Lars Madsen
Post by GL
Post by GL
Post by Lars Madsen
Post by GL
Post by GL
Post by Lars Madsen
to make things simpler we make use of
\lstnewenvironment
so perhaps some local redefinitions may help
listings basically test, as expected,
\begin{lstlisting}[caption={\hskip1sp}]
can handle this case ;-)
Or may be with more sense: caption=\null
but this leaves the : which we wanted to be removed when the caption is
empty
caption={\hskip1sp}
will also give 'Listing 1:' if the caption package is used
\vskip\abovecaptionskip
\else
\fi
\vskip\belowcaptionskip}%
\begin{lstlisting}[caption=\null]
coucou
\end{lstlisting}
\vskip\abovecaptionskip
\else
\fi
\vskip\belowcaptionskip}%
produces the desired result (\kern0pt, \unskip, \null etc.)
doesnt' that defy the purpose of using the caption package to control
the font etc. of the caption?
I'm not fond of the caption package etc. to control the font.
A command such \@makecaption is very simple and if a package like
caption.sty does not provide a way to choose to insert or not a
colon, then my opinion is that such a package should be avoided.

Another serious reason is that the caption package has no documentation:
the documentation is the documented code, in other words, no interface
is provided.

Regards.
Post by Lars Madsen
it is being used, may help.
come to think of it it might even be useful (for my application) to have
more control of what is written to a 'list of' file, in which case I
Enrico Gregorio
2011-07-21 13:26:47 UTC
Permalink
Post by GL
the documentation is the documented code, in other words, no interface
is provided.
It has a 58 page user documentation; it's called
caption-eng.pdf, but "texdoc caption" finds it.
It'a available also in German (caption-deu.pdf)
and Russian (caption-rus.pdf).

Ciao
Enrico
Ulrike Fischer
2011-07-21 16:29:35 UTC
Permalink
Post by Enrico Gregorio
Post by GL
the documentation is the documented code, in other words, no interface
is provided.
It has a 58 page user documentation; it's called
caption-eng.pdf, but "texdoc caption" finds it.
It'a available also in German (caption-deu.pdf)
and Russian (caption-rus.pdf).
Yes. And nobody seems to read it ;-). caption has an interface for
listings and so to get rid of the colon and the caption text is
easy:

\documentclass[paper=A4,pagesize]{scrartcl}
\usepackage{listings}
\usepackage{caption}
\DeclareCaptionTextFormat{gobble}{}
\captionsetup[lstlisting]{labelsep=none,textformat=gobble}

\begin{document}

\begin{lstlisting}[caption={some text}]
abc
\end{lstlisting}
\end{document}
--
Ulrike Fischer
Lars Madsen
2011-07-21 17:58:24 UTC
Permalink
Post by Ulrike Fischer
\documentclass[paper=A4,pagesize]{scrartcl}
\usepackage{listings}
\usepackage{caption}
\DeclareCaptionTextFormat{gobble}{}
\captionsetup[lstlisting]{labelsep=none,textformat=gobble}
\begin{document}
\begin{lstlisting}[caption={some text}]
abc
\end{lstlisting}
\end{document}
nice to know

but that is still not what we want

caption=

should give

Listings 1

And

caption=text

should give

Listings 1: text

(or similar)

/daleif
Ulrike Fischer
2011-07-22 07:41:49 UTC
Permalink
Post by Lars Madsen
Post by Ulrike Fischer
\documentclass[paper=A4,pagesize]{scrartcl}
\usepackage{listings}
\usepackage{caption}
\DeclareCaptionTextFormat{gobble}{}
\captionsetup[lstlisting]{labelsep=none,textformat=gobble}
\begin{document}
\begin{lstlisting}[caption={some text}]
abc
\end{lstlisting}
\end{document}
nice to know
but that is still not what we want
caption=
should give
Listings 1
And
caption=text
should give
Listings 1: text
(or similar)
/daleif
\documentclass[paper=A4,pagesize]{scrartcl}
\usepackage{listings}
\makeatletter
\***@key{lst}{nocaption}[]{%
\setkeys{lst}{caption=blub}%
\captionsetup[lstlisting]{labelsep=none,textformat=gobble}}
\makeatother
\usepackage{caption}
\DeclareCaptionTextFormat{gobble}{}
\captionsetup[lstlisting]{labelsep=colon}

\begin{document}

\begin{lstlisting}[caption={some text}]
abc
\end{lstlisting}

\begin{lstlisting}[nocaption]
cde
\end{lstlisting}


\begin{lstlisting}
xy
\end{lstlisting}

\begin{lstlisting}[caption=test]
cde
\end{lstlisting}

\end{document}
--
Ulrike Fischer
Lars Madsen
2011-07-22 08:32:03 UTC
Permalink
Post by Ulrike Fischer
Post by Lars Madsen
Post by Ulrike Fischer
\documentclass[paper=A4,pagesize]{scrartcl}
\usepackage{listings}
\usepackage{caption}
\DeclareCaptionTextFormat{gobble}{}
\captionsetup[lstlisting]{labelsep=none,textformat=gobble}
\begin{document}
\begin{lstlisting}[caption={some text}]
abc
\end{lstlisting}
\end{document}
nice to know
but that is still not what we want
caption=
should give
Listings 1
And
caption=text
should give
Listings 1: text
(or similar)
/daleif
\documentclass[paper=A4,pagesize]{scrartcl}
\usepackage{listings}
\makeatletter
\setkeys{lst}{caption=blub}%
\captionsetup[lstlisting]{labelsep=none,textformat=gobble}}
\makeatother
\usepackage{caption}
\DeclareCaptionTextFormat{gobble}{}
\captionsetup[lstlisting]{labelsep=colon}
\begin{document}
\begin{lstlisting}[caption={some text}]
abc
\end{lstlisting}
\begin{lstlisting}[nocaption]
cde
\end{lstlisting}
\begin{lstlisting}
xy
\end{lstlisting}
\begin{lstlisting}[caption=test]
cde
\end{lstlisting}
\end{document}
Thanks, interesting solution, have to admit I've never been that far
down captions possibilities.

On our application we use custom listings environments, and would like
to have our envs autonumbered.

If one play a little with your solution, then that is easily done:

\documentclass[paper=A4,pagesize]{scrartcl}
\usepackage{listings,etoolbox}
\makeatletter

\newtoggle{capactive}
% what caption normally does
\***@Key{nocaption}\relax{\***@OptArg[{#1}]{#1}%
{\def\***@caption{##2}\def\lst@@caption{##1}}%
\let\***@title\@empty}
% make sure we know when a caption is being used
\***@Key{caption}\relax{\***@OptArg[{#1}]{#1}%
{\def\***@caption{##2}\def\lst@@caption{##1}}%
\toggletrue{capactive}%
\let\***@title\@empty}

\makeatother
\usepackage{caption}
\DeclareCaptionTextFormat{gobble}{}
\captionsetup[lstlisting]{labelsep=colon}
\lstnewenvironment{myoutput}[1][]{%
\lstset{nocaption=x,#1}
% only use the special caption setup if the caption is not active
\iftoggle{capactive}{}{%
\captionsetup[lstlisting]{labelsep=none,textformat=gobble}}
}{}
\begin{document}

\begin{myoutput}
sdfsf
\end{myoutput}
\begin{myoutput}[caption=test]
sdfsf
\end{myoutput}

\end{document}
--
/daleif (remove RTFSIGNATURE from email address)

Memoir and mh bundle maintainer
LaTeX FAQ: http://www.tex.ac.uk/faq
LaTeX book: http://www.imf.au.dk/system/latex/bog/ (in Danish)
Remember to post minimal examples, see URL below
http://www.minimalbeispiel.de/mini-en.html
Lars Madsen
2011-07-21 13:32:36 UTC
Permalink
Post by GL
I'm not fond of the caption package etc. to control the font.
caption.sty does not provide a way to choose to insert or not a
colon, then my opinion is that such a package should be avoided.
the documentation is the documented code, in other words, no interface
is provided.
Regards.
the caption package does provide this feature

if the caption package is used then a \caption{} in a figure env gives you

'Figure 1' not 'Figure 1:'

but with listings it never get that far because the caption is stopped
when the option is 'caption=' (or similar) and then never get passed on
to \@makecaption (if it makes it that far it does work fine)
--
/daleif (remove RTFSIGNATURE from email address)

Memoir and mh bundle maintainer
LaTeX FAQ: http://www.tex.ac.uk/faq
LaTeX book: http://www.imf.au.dk/system/latex/bog/ (in Danish)
Remember to post minimal examples, see URL below
http://www.minimalbeispiel.de/mini-en.html
Lars Madsen
2011-07-21 14:07:22 UTC
Permalink
Post by Lars Madsen
Post by GL
I'm not fond of the caption package etc. to control the font.
caption.sty does not provide a way to choose to insert or not a
colon, then my opinion is that such a package should be avoided.
the documentation is the documented code, in other words, no interface
is provided.
Regards.
the caption package does provide this feature
if the caption package is used then a \caption{} in a figure env gives you
'Figure 1' not 'Figure 1:'
but with listings it never get that far because the caption is stopped
when the option is 'caption=' (or similar) and then never get passed on
Here is something that seem to be working (sorry on my way out the door,
so a full MWE will have to wait for later)

This require etoolbox (and I have not gotten to the 'list of' part yet),
combine with loading caption as well and caption= => Listing 1, not
'Listing 1:'

\def\***@MakeCaption#1{%
\***@ifdisplaystyle
\ifx #1t%
% \ifx\lst@@caption\@empty
% \expandafter\***@HRefStepCounter
% \else
% \expandafter\refstepcounter
% \fi
% this might not be completely correct
\iftoggle{capactive}{%
\expandafter\refstepcounter%
}{%
\expandafter\***@HRefStepCounter%
}%
{lstlisting}%
\ifx\***@label\@empty\else \label{\***@label}\fi
\let\***@arg\***@intname \***@ReplaceIn\***@arg\***@filenamerpl
\global\let\***@name\***@arg \global\let\lstname\***@name
\***@ifnolol\else
\ifx\lst@@caption\@empty
\ifx\***@caption\@empty
\ifx\***@intname\@empty
\else
\def\***@temp{ }%
\ifx\***@intname\***@temp
\else
\addcontentsline{lol}{lstlisting}\***@name
\fi
\fi
\fi
\else
\addcontentsline{lol}{lstlisting}%
{\protect\numberline{\thelstlisting}\lst@@caption}%
\fi
\fi
\fi
% \ifx\***@caption\@empty\else
\iftoggle{capactive}{%
\***@IfSubstring #1\***@captionpos
{\begingroup \let\@@vskip\vskip
\def\vskip{\afterassignment\***@vskip \@tempskipa}%
\def\***@vskip{\nobreak\@@vskip\@tempskipa\nobreak}%
\par\@parboxrestore\normalsize\normalfont % \noindent (AS)
\ifx #1t\allowbreak \fi
\ifx\***@title\@empty
\ifdefvoid\***@caption{%
\***@makecaption\***@lstlisting{}%
}{%

\***@makecaption\***@lstlisting{\ignorespaces\***@caption}%
}%
\else
\***@maketitle\***@title % (AS)
\fi
\ifx #1b\allowbreak \fi
\endgroup}{}%
% \fi
}{}%
\fi}

\newtoggle{capactive}
\***@Key{caption}\relax{\***@OptArg[{#1}]{#1}%
{\def\***@caption{##2}\def\lst@@caption{##1}}%
\toggletrue{capactive}%
\let\***@title\@empty}

\def\***@lstlisting{%
\lstlistingname%
%\ifx\lst@@caption\@empty
%\else
~\thelstlisting%
% \fi
}%
--
/daleif (remove RTFSIGNATURE from email address)

Memoir and mh bundle maintainer
LaTeX FAQ: http://www.tex.ac.uk/faq
LaTeX book: http://www.imf.au.dk/system/latex/bog/ (in Danish)
Remember to post minimal examples, see URL below
http://www.minimalbeispiel.de/mini-en.html
Enrico Gregorio
2011-07-21 12:22:06 UTC
Permalink
Post by Lars Madsen
Hi does anyone have an idea as to how one can get listings to work with
empty captions?
As it is now if one use
\begin{lstlisting}[caption={}]
test
\end{lstlisting}
then no caption will be printed
We would like to be able to get a header/caption in the manner of
or if say the caption package is used
Listing 1
(i.e. no colon)
Fool listings by saying "caption={\kern1sp}"

There are many \ifx\lst@@caption\@empty in the definition
of \***@MakeCaption, so I guess that a complete redefinition
of this command should be needed to get it automatically.

Ciao
Enrico
Lars Madsen
2011-07-21 12:31:34 UTC
Permalink
Post by Enrico Gregorio
Post by Lars Madsen
Hi does anyone have an idea as to how one can get listings to work with
empty captions?
As it is now if one use
\begin{lstlisting}[caption={}]
test
\end{lstlisting}
then no caption will be printed
We would like to be able to get a header/caption in the manner of
or if say the caption package is used
Listing 1
(i.e. no colon)
Fool listings by saying "caption={\kern1sp}"
of this command should be needed to get it automatically.
Ciao
Enrico
thanks but this still leaves the ':'
--
/daleif (remove RTFSIGNATURE from email address)

Memoir and mh bundle maintainer
LaTeX FAQ: http://www.tex.ac.uk/faq
LaTeX book: http://www.imf.au.dk/system/latex/bog/ (in Danish)
Remember to post minimal examples, see URL below
http://www.minimalbeispiel.de/mini-en.html
Enrico Gregorio
2011-07-21 12:51:13 UTC
Permalink
Post by Lars Madsen
Post by Enrico Gregorio
Post by Lars Madsen
Hi does anyone have an idea as to how one can get listings to work with
empty captions?
As it is now if one use
\begin{lstlisting}[caption={}]
test
\end{lstlisting}
then no caption will be printed
We would like to be able to get a header/caption in the manner of
or if say the caption package is used
Listing 1
(i.e. no colon)
Fool listings by saying "caption={\kern1sp}"
of this command should be needed to get it automatically.
Ciao
Enrico
thanks but this still leaves the ':'
\begin{lstlisting}[title=\refstepcounter{lstlisting}%
\lstlistingname~\arabic{lstlisting}]

Doesn't work with "label", though.

Ciao
Enrico
Lars Madsen
2011-07-21 12:55:03 UTC
Permalink
Post by Enrico Gregorio
Post by Lars Madsen
Post by Enrico Gregorio
Post by Lars Madsen
Hi does anyone have an idea as to how one can get listings to work with
empty captions?
As it is now if one use
\begin{lstlisting}[caption={}]
test
\end{lstlisting}
then no caption will be printed
We would like to be able to get a header/caption in the manner of
or if say the caption package is used
Listing 1
(i.e. no colon)
Fool listings by saying "caption={\kern1sp}"
of this command should be needed to get it automatically.
Ciao
Enrico
thanks but this still leaves the ':'
\begin{lstlisting}[title=\refstepcounter{lstlisting}%
\lstlistingname~\arabic{lstlisting}]
Doesn't work with "label", though.
Ciao
Enrico
and it does not allow for configuration from the caption package

I think the best solution is to hack

\***@MakeCaption

and

\***@Key{caption}

it is not that many places in listings that \***@caption or
\lst@@caption are being used
--
/daleif (remove RTFSIGNATURE from email address)

Memoir and mh bundle maintainer
LaTeX FAQ: http://www.tex.ac.uk/faq
LaTeX book: http://www.imf.au.dk/system/latex/bog/ (in Danish)
Remember to post minimal examples, see URL below
http://www.minimalbeispiel.de/mini-en.html
Enrico Gregorio
2011-07-21 12:34:52 UTC
Permalink
Post by Enrico Gregorio
Post by Lars Madsen
Hi does anyone have an idea as to how one can get listings to work with
empty captions?
As it is now if one use
\begin{lstlisting}[caption={}]
test
\end{lstlisting}
then no caption will be printed
We would like to be able to get a header/caption in the manner of
or if say the caption package is used
Listing 1
(i.e. no colon)
Fool listings by saying "caption={\kern1sp}"
of this command should be needed to get it automatically.
Better yet!

caption={\unskip}

Ciao
Enrico
Continue reading on narkive:
Loading...