4

I'm trying to put an hyphen after the entries of the "List of Listings" as follows:

List of Codes

Code 1 -- First Code ................................ 7

Code 2 -- Second Code ............................... 8

With the following code ...

\documentclass[12pt,openright,oneside,dvipsnames,a4paper,%
chapter=TITLE,section=TITLE,english]{abntex2}
\usepackage{textcomp}
\usepackage{caption} 
\usepackage{listings}

\renewcommand{\lstlistingname}{Code}         %  Listings
\renewcommand{\lstlistlistingname}{List of Codes}%

\lstset{
            language = [LaTeX]{TeX}
}

\AtBeginDocument{ %
            \counterwithout{lstlisting}{chapter}
}

\begin{document}

\lstinputlisting[caption={First Code}]{code1.tex}
\lstinputlisting[caption={Second Code}]{code2.tex}

\end{document}

I have:

List of Codes

Code 1 First Code ................................ 7

Code 2 Second Code ............................... 8

Thanks in advance for any help.

4

Here's one possibility using the tocloft package and part of egreg's answer to Customizing the list of listings generated by \lstlistoflistings?:

\documentclass[12pt,openright,oneside,dvipsnames,a4paper,%
chapter=TITLE,section=TITLE,english]{abntex2}
\usepackage{textcomp}
\usepackage{caption} 
\usepackage{listings}
\usepackage{tocloft}

\renewcommand\lstlistingname{Code}         %  Listings
\renewcommand\lstlistlistingname{List of Codes}%

\lstset{
  language = [LaTeX]{TeX}
}

\AtBeginDocument{ %
  \counterwithout{lstlisting}{chapter}
}

\newlength\mylen

\begingroup
\makeatletter
\let\newcounter\@gobble\let\setcounter\@gobbletwo
  \globaldefs\@ne \let\c@loldepth\@ne
  \newlistof{listings}{lol}{\lstlistlistingname}
  \newlistentry{lstlisting}{lol}{0}
\makeatother
\endgroup

\renewcommand\cftlstlistingpresnum{\lstlistingname~}
\settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}
\addtolength\cftlstlistingnumwidth{\mylen} %
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}

\begin{document}

\lstlistoflistings

\begin{lstlisting}[caption={First Code}]
code1
\end{lstlisting}

\begin{lstlisting}[caption={Second Code}]
code2
\end{lstlisting}

\end{document}

enter image description here

  • Thanks Gonzalo. I've got better result with {\hfill\textendash\hfill} instead of {\,--} in \renewcommand\cftlstlistingaftersnum{\,--}. – Ulysses Feb 20 '15 at 1:24
  • @Ulysses You're welcome. Yes, that gives better results. I edited my answer to include it. – Gonzalo Medina Feb 20 '15 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.