3

I would like to change the original Listing 1.1: Caption style to 1.1. listing. Caption.

I've already found two possible solutions to this problem in the following thread: How can I reverse the caption numbering in listing. Unfortunately, none of them works for me. I suspect this is because the very specific way I am using listings.

Here is my config (that I created from lots of TexStackExchange posts) with a minimal working example.

\documentclass[12pt,a4paper,oneside]{report}

\usepackage{t1enc}
\usepackage[english,magyar]{babel}
\usepackage[utf8]{inputenc}
\usepackage[chapter]{minted}
\usepackage{tcolorbox}                  % https://tex.stackexchange.com/questions/305387/adding-a-caption-to-a-tcolorbox-tcblisting

\tcbuselibrary{listings,minted,skins,breakable}
\renewcommand*\thelstnumber{\ifnum\value{lstnumber}<10 0\fi\the\value{lstnumber}}
\renewcommand{\lstlistingname}{Kódrészlet}
\renewcommand{\listoflistingscaption}{Kódrészletek jegyzéke}

\makeatletter
\renewcommand\fnum@lstlisting{%
  \ifx\lst@@caption\@empty\else\thelstlisting~\fi%
  \lstlistingname}%
\makeatother

\makeatletter
\AtBeginDocument{\let\c@listing\c@lstlisting}
\makeatother

\AtBeginDocument{%
  \newtcblisting[blend into=listings]{code}[3]{%
    breakable,
    colback=codebg,
    colframe=black!40,
    enhanced,
    listing engine=minted,
    listing only,
    left=5mm,
    overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
      rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}},
    listing remove caption=false,
    minted style=colorful,
    minted language=#1,
    minted options={linenos=true,numbersep=3mm,texcl=true,breaklines=true,autogobble=true},
    coltitle=black,
    attach boxed title to bottom center={yshift=-10pt},
    boxed title style={enhanced jigsaw, colback=white, sharp corners, boxrule=0pt},
    #2
  }
}
\definecolor{codebg}{rgb}{0.95,0.95,0.95}

\newcommand{\listAref}[1]{\Aref{lst:#1}. kódrészlet}

\begin{document}

\tableofcontents\vfill

\newpage
\begin{otherlanguage}{english}
  \begin{code}{swift}{title={Test title}, label={lst:test-code}}

    import Foundation
    import UIKit

    class ViewController: UIViewController {

      override func viewDidLoad() {
        super.viewDidLoad()

      }

    }

  \end{code}
\end{otherlanguage}

\listAref{test-code} egy tesztkódot mutat.

\listoflistings\addcontentsline{toc}{chapter}{Kódrészletek jegyzéke}

\end{document}

Everything is working fine but the fnum@lstlisting seems to be ignored(?). What am I doing wrong?

2
  • Welcome to TeX.SX. Instead of code fragments, it's better that you build a minimal but complete working or not example. From \documentclass to \end{document}. This way possible helpers won't have to imagine what kind of document are you compiling or what packages are you using.
    – Ignasi
    Dec 9, 2016 at 8:51
  • @Ignasi thank you! I extended my question with a simple working example.
    – farkasseb
    Dec 9, 2016 at 9:55

2 Answers 2

2

I made two changes to treat the problem.

Firstly, an alternative to blend into=listings is constructed by

\tcbset{
  new/blend into/revlistings/.style={use counter*=lstlisting,list inside=lol,/tcb/code={\appto\tcb@new@colopt{,before title={\tcb@blend@beforetitle{\thetcbcounter~\lstlistingname}}}}},%
}

The new blend into=revlistings reverses the number with the name.

Secondly,

blend before title code={##1.\ },

is added to exchange the : by . after the number+name text.

enter image description here

The full code is:

\documentclass[12pt,a4paper,oneside]{report}

\usepackage{t1enc}
\usepackage[english,magyar]{babel}
\usepackage[utf8]{inputenc}
\usepackage[chapter]{minted}
\usepackage{tcolorbox}                  % http://tex.stackexchange.com/questions/305387/adding-a-caption-to-a-tcolorbox-tcblisting

\tcbuselibrary{listings,minted,skins,breakable}
\renewcommand*\thelstnumber{\ifnum\value{lstnumber}<10 0\fi\the\value{lstnumber}}
\renewcommand{\lstlistingname}{Kódrészlet}
\renewcommand{\listoflistingscaption}{Kódrészletek jegyzéke}


\makeatletter
\renewcommand\fnum@lstlisting{%
  \ifx\lst@@caption\@empty\else\thelstlisting~\fi%
  \lstlistingname}%
\tcbset{
  new/blend into/revlistings/.style={use counter*=lstlisting,list inside=lol,/tcb/code={\appto\tcb@new@colopt{,before title={\tcb@blend@beforetitle{\thetcbcounter~\lstlistingname}}}}},%
}
\makeatother

\makeatletter
\AtBeginDocument{\let\c@listing\c@lstlisting}
\makeatother

\AtBeginDocument{%
  \newtcblisting[blend into=revlistings]{code}[2]{%
    blend before title code={##1.\ },
    breakable,
    colback=codebg,
    colframe=black!40,
    enhanced,
    listing engine=minted,
    listing only,
    left=5mm,
    overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
      rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}},
    listing remove caption=false,
    minted style=colorful,
    minted language=#1,
    minted options={linenos=true,numbersep=3mm,texcl=true,breaklines=true,autogobble=true},
    coltitle=black,
    attach boxed title to bottom center={yshift=-10pt},
    boxed title style={enhanced jigsaw, colback=white, sharp corners, boxrule=0pt},
    #2
  }
}
\definecolor{codebg}{rgb}{0.95,0.95,0.95}

\newcommand{\listAref}[1]{\Aref{lst:#1}. kódrészlet}

\begin{document}

\tableofcontents\vfill

\newpage

\begin{otherlanguage}{english}
  \begin{code}{swift}{title={Test title}, label={lst:test-code}}

    import Foundation
    import UIKit

    class ViewController: UIViewController {

      override func viewDidLoad() {
        super.viewDidLoad()

      }

    }

  \end{code}
\end{otherlanguage}

\listAref{test-code} egy tesztkódot mutat.

\listoflistings\addcontentsline{toc}{chapter}{Kódrészletek jegyzéke}

\end{document}
2
  • Thank you, it does exactly what I wanted! The blend before title code={##1.\ }, wasn't necessary for me. I would lie if I say that I understand your tcbset. Could you please give an explanation?
    – farkasseb
    Dec 14, 2016 at 1:41
  • @farkasseb new/blend into/revlistings creates an option with is usable as intialization option (this is the new branch). Basically, it is a copy of the normal new/blend into/listings key you used before. The difference is before title={\tcb@blend@beforetitle{\thetcbcounter~\lstlistingname}} - here, the counter and listing name sequence is switched. This stuff is put before every title. The internal macro \tcb@blend@beforetitle only adds some decorations like the :. Dec 14, 2016 at 15:00
2

This answer won't completely solve the problem but I hope it helps.

I couldn't find in tcolorbox documentation any other option for reversing box number and name than theorem number and name option from theorems library. But it doesn't work in tcblistings and I don't know how to convert a tcbtheorem into a tcblisting.

So I propose to use tcblisting to format the code, but instead of using it as a float, create a new float (\usepackage{newfloat}) which will be used to include non floating tcblistings. In this case the caption will correspond to the newfloat type and you can reverse it with Thorsten Donig solution to linked question.

Some code to start working with:

\documentclass[12pt,a4paper,oneside]{report}

\usepackage{t1enc}
\usepackage[english,magyar]{babel}
\usepackage[utf8]{inputenc}
\usepackage[chapter]{minted}
\usepackage{tcolorbox}              
\usepackage{newfloat}

\DeclareFloatingEnvironment[%
    listname={List of My Listings},
    name=MyListing,
    fileext=loml]{mylisting}

\usepackage{caption}
\DeclareCaptionLabelFormat{reverse}{#2 #1}
\captionsetup{labelformat=reverse}

\tcbuselibrary{listings,minted,skins,breakable}

\newtcblisting{code}[2][]{%
    breakable,
    colback=codebg,
    colframe=black!40,
    enhanced,
    listing engine=minted,
    listing only,
    left=5mm,
    overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
      rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}},
    minted style=colorful,
    minted language=#2,
    minted options={linenos=true,numbersep=3mm,texcl=true,breaklines=true,autogobble=true},
    coltitle=black,
    #1
  }


\definecolor{codebg}{rgb}{0.95,0.95,0.95}

%\newcommand{\listAref}[1]{\Aref{lst:#1}. kódrészlet}

\begin{document}

\begin{mylisting}
\begin{otherlanguage}{english}
\begin{tcblisting}{
    enhanced,
    breakable, 
    colback=codebg,
    colframe=black!40,
    listing engine=minted,
    listing only,
    left=5mm,
    minted style=colorful,
    minted language=swift,
    minted options={linenos=true, numbersep=3mm, texcl=true,
            breaklines=true, autogobble=true},
    overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
      rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}},
}
    import Foundation
    import UIKit

    class ViewController: UIViewController {

      override func viewDidLoad() {
        super.viewDidLoad()

      }

    }
\end{tcblisting}
\end{otherlanguage}
\caption{First}
\end{mylisting}

\end{document}

enter image description here

1
  • Thank you very much for your help! I chose Thomas's answer because it seemed to be easier to integrate into my thesis (the deadline was quickly approaching). Your solution however is more clean (or at least I can understand the reasoning behind it).
    – farkasseb
    Dec 14, 2016 at 1:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .