如何用其他东西替换命令参数#2?

如何用其他东西替换命令参数#2?

我正在尝试修补这个宏:

\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

有了这个,它可以工作,但这不是我想要的:

\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}

我确实用 替换了\addtocontents\addtocontents{lstlisting}但是,我想用 替换\addtocontents{#2}\addtocontents{lstlisting}但如果我使用\addtocontents{#2}作为搜索参数\xpatchcmd,则补丁不起作用。

有关的:

  1. 调整 KOMA 中标题和列表之间的垂直间距(scrartcl)

答案1

我设法通过以下方式修复了它@egreg注释,我只重新定义了\contentsline命令:

% 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
}{}

相关内容