0

I am trying to patch this macro:

\xshowcmd\addcontentsline

> \addcontentsline=macro:
#1#2#3->\begingroup \let \label \@gobble \ifx \@currentHref \@empty \Hy@Warning
 {No destination for bookmark of \string \addcontentsline ,\MessageBreak destin
ation is added}\phantomsection \fi \expandafter \ifx \csname toclevel@#2\endcsn
ame \relax \begingroup \def \Hy@tempa {#1}\ifx \Hy@tempa \Hy@bookmarkstype \Hy@
WarningNoLine {bookmark level for unknown #2 defaults to 0}\else \Hy@Info {book
mark level for unknown #2 defaults to 0}\fi \endgroup \expandafter \gdef \csnam
e toclevel@#2\endcsname {0}\fi \edef \Hy@toclevel {\csname toclevel@#2\endcsnam
e }\Hy@writebookmark {\csname the#2\endcsname }{#3}{\@currentHref }{\Hy@tocleve
l }{#1}\ifHy@verbose \begingroup \def \Hy@tempa {#3}\@onelevel@sanitize \Hy@tem
pa \let \temp@online \on@line \let \on@line \@empty \Hy@Info {bookmark\temp@onl
ine :\MessageBreak thecounter {\csname the#2\endcsname }\MessageBreak text {\Hy
@tempa }\MessageBreak reference {\@currentHref }\MessageBreak toclevel {\Hy@toc
level }\MessageBreak type {#1}}\endgroup \fi \addtocontents {#1}{\protect \cont
entsline{#2}{#3}{\thepage }{\@currentHref }}\endgroup .
<recently read> \addcontentsline

With this, which works, but it is not what I want:

\documentclass[english,12pt,a4paper,twoside]{abntex2}
\usepackage{caption,xpatch,listings}
\usepackage[newfloat,chapter]{minted}
\makeatletter

% https://tex.stackexchange.com/questions/320185/adjust-vertical-spacing-between-caption-and-listing-in-koma-scrartcl
\captionsetup[listing]{position=top,skip=0pt}
\tracingpatches

\newenvironment{code}{
  \catcode`\#=12
  \xpatchcmd{\addcontentsline}
  {\contentsline{##2}}
  {\contentsline{lstlisting}}
  {\SUCCESSFULLYPATHED}
  {\FAILEDPATCH}
  \catcode`\#=6

  \xshowcmd\addcontentsline
}{}

% https://tex.stackexchange.com/questions/26220/control-spacing-around-table-caption
\AtBeginEnvironment{code}{\setcounter{listing}{\value{lstlisting}}
\setlength{\abovecaptionskip}{0pt}
\setlength{\belowcaptionskip}{10pt}
\setlength{\parskip}{0pt}
\captionsetup{skip=0pt}
}
\AtEndEnvironment{code}{\stepcounter{lstlisting}}

% https://tex.stackexchange.com/questions/269491/mixing-minted-with-lstlisting
\AtBeginEnvironment{listing}{\setcounter{listing}{\value{lstlisting}}}
\AtEndEnvironment{listing}{\stepcounter{lstlisting}}

  \newlength\mylen

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

  % Why the empty space size is increasing each call to my calculate listing header command?
  % https://tex.stackexchange.com/questions/388411/why-the-empty-space-size-is-increasing-each-call
  \newlength\cftlstlistingoldnumwidth
  \setlength\cftlstlistingoldnumwidth{\cftlstlistingnumwidth}

  % Calculate the size of the header
  % What is the use of percent signs (%) at the end of lines?
  % https://tex.stackexchange.com/questions/7453/what-is-the-use-of-percent-signs-at-the-end-of-lines
  \newcommand{\calculatelisteningsheader}
  {%
      \renewcommand\cftlstlistingpresnum{\lstlistingname~}%
      \settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}%
      \setlength\cftlstlistingnumwidth{\dimexpr\cftlstlistingoldnumwidth+\mylen}%
      \renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}%
  }

  % Ensure it is called at least one time
  \calculatelisteningsheader

  % https://tex.stackexchange.com/questions/14135/how-to-automatically-add-text-immediately-after-begindocument
  \AtBeginDocument{\calculatelisteningsheader}
\makeatother

\begin{document}

{\renewcommand{\cftfigurename}{\listingname\space}
\calculatelisteningsheader
\lstlistoflistings
}
% \newpage

\begin{lstlisting}[caption={Listing}]
# If the body of the namespace is longer than this
# number, it won't be indented. Requires
\end{lstlisting}
% \newpage

\begin{code}
\caption{Code}
\begin{minted}{text}
$ python3 unit_tests.py
OK
\end{minted}
\end{code}
% \newpage

\begin{lstlisting}[caption={Listing}]
# If the body of the namespace is longer than this
# number, it won't be indented. Requires
\end{lstlisting}
% \newpage

\begin{code}
\caption{Code}
\begin{minted}{text}
$ python3 unit_tests.py
OK
\end{minted}
\end{code}
% \newpage

\end{document}

I does replaces the \addtocontents with \addtocontents{lstlisting}, however, I would like to replace the \addtocontents{#2} with \addtocontents{lstlisting}. But if I use \addtocontents{#2} as the search argument for \xpatchcmd, the patch does not work.

Related:

  1. Adjust vertical spacing between caption and listing in KOMA (scrartcl)
5
  • I'd suggest \newcommand{\specialcontentsline}[1]{\contentsline{lstlisting}} and then the patch can be \xpatchcmd{\addcontentsline}{\contentsline}{\specialcontentsline}{}{}. There may be better ways to do the same, can you please add a minimal example?
    – egreg
    Nov 3, 2019 at 23:30
  • You need to move the \catcode`\#=12...\catcode`\#=6 outside the \newenvironment. But it's probably a bad idea to patch \addcontentsline. Go with egreg's suggestion. Nov 3, 2019 at 23:49
  • @egreg I added an minimal example. @PhelypeOleinik, I am just patching it for my custom environment. By replacing \contentline{#2} with \contentline{lstlisting}, this would fix my other problem, because code environment toc is being generated with listing instead of lstlisting.
    – user
    Nov 3, 2019 at 23:54
  • Sorry, but the example is too minimal.
    – egreg
    Nov 4, 2019 at 0:09
  • @egreg, I unminimized it, then.
    – user
    Nov 4, 2019 at 0:14

1 Answer 1

0

I managed to fix it by following @egreg comments, and I redefined only the \contentsline command:

% https://tex.stackexchange.com/questions/514715/how-to-fix-minted-and-listings
\newenvironment{code}{
  \captionsetup{type=listing}

  % https://tex.stackexchange.com/questions/514847/xpatch-fails-if
  \def\makesafecoloncatcoderestore##1{%
      \edef##1{\catcode`:=\the\catcode`:\relax}%
  }

  % https://tex.stackexchange.com/questions/335104/save-and-restore-the
  \makesafecoloncatcoderestore\restorecodecoloncatcode
  \catcode`\:=12

  \xpatchcmd{\addcontentsline}
  {\contentsline}
  {\specialcodelistingcontentsline}{
    % \message{^^J^^JCould YES patch the contentsline environment!^^J^^J^^J^^J^^J}
  }
  {\FAILEDPATCHSPECIALCONTENTSLINE}

  % \xshowcmd\addcontentsline
  \restorecodecoloncatcode
}{}
1
  • Note: Using this, i.e., replacing the contents of main.lol will cause all links created by hyperref to not be created and several listings will not be linked to the right page.
    – user
    Nov 6, 2019 at 23:36

You must log in to answer this question.

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