让新的 tcblisting TOC 与当前 TOC 相匹配

让新的 tcblisting TOC 与当前 TOC 相匹配

在下面的代码中,我尝试将新内容的格式和字体\tcblistof与我的其他目录进行匹配

我的新\tcblistof外观如下所示,缺少数字 1.1: 在此处输入图片描述

我希望它能与我现有的 TOC 格式相匹配,就像这样...

在此处输入图片描述

这是我目前拥有的代码:

\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}

\usepackage{calc}
\usepackage{graphicx, rotating, ltablex, dcolumn, booktabs}
\usepackage{pdflscape}
\usepackage[usenames,dvipsnames,svgnames,table,xcdraw]{xcolor}
\usepackage{tikz}
\usepackage{eso-pic}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}
\usepackage{listings}
\usepackage{xparse}
\usepackage{titletoc}
\usepackage{avant}
\usepackage{microtype}
\usepackage[T1]{fontenc}

\newcommand{\codeimg}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-a}}}$}
\newcommand*{\maincolor}{blue}

%======================================================================================
%   CHAPTER HEADINGS
%======================================================================================

\newcommand{\listofmatlabname}{List of Matlab Code}
\newcommand{\listofresultname}{List of Results}

\usepackage{etoolbox,fancyhdr}

%======================================================================================
%   MAIN TABLE OF CONTENTS
%======================================================================================

\contentsmargin{0cm}

% Part text styling
\titlecontents{part}[0cm]
{\addvspace{20pt}\centering\large\bfseries}
{}
{}
{}

% Chapter text styling
\titlecontents{chapter}[1.25cm] % Indentation
{\addvspace{12pt}\large\sffamily\bfseries} % Spacing and font options for chapters
{\color{\maincolor!60}\contentslabel[\Large\thecontentslabel]{1.25cm}\color{\maincolor}} % Chapter number
{\color{\maincolor}}
{\color{\maincolor!60}\normalsize\;\titlerule*[.5pc]{.}\;\thecontentspage} % Page number

% Section text styling
\titlecontents{section}[1.25cm] % Indentation
{\addvspace{3pt}\sffamily\bfseries} % Spacing and font options for sections
{\contentslabel[\thecontentslabel]{1.25cm}} % Section number
{}
{\hfill\color{black}\thecontentspage} % Page number
[]

% Subsection text styling
\titlecontents{subsection}[1.25cm] % Indentation
{\addvspace{1pt}\sffamily\small} % Spacing and font options for subsections
{\contentslabel[\thecontentslabel]{1.25cm}} % Subsection number
{}
{\ \titlerule*[.5pc]{.}\;\thecontentspage} % Page number
[]

% List of figures
\titlecontents{figure}[0em]
{\addvspace{2pt}\sffamily}
{\thecontentslabel\hspace*{1em}}
{}
{\ \titlerule*[.5pc]{.}\;\thecontentspage}
[]

% List of tables
\titlecontents{table}[0em]
{\addvspace{2pt}\sffamily}
{\thecontentslabel\hspace*{1em}}
{}
{\ \titlerule*[.5pc]{.}\;\thecontentspage}
[]

% List of listings
\contentsuse{lstlisting}{lol}
\titlecontents{lstlisting}[0em]
  {\addvspace{5pt}\sffamily}
  {\thecontentslabel\hspace*{1em}}
  {}
  {\ \titlerule*[.5pc]{.}\;\thecontentspage}
  []

% List of Matlab
\titlecontents{matlab}[0em]
{\addvspace{2pt}\sffamily}
{\thecontentslabel\hspace*{1em}}
{}
{\ \titlerule*[.5pc]{.}\;\thecontentspage}
[]

% List of results
\titlecontents{result}[0em]
{\addvspace{2pt}\sffamily}
{\thecontentslabel\hspace*{1em}}
{}
{\ \titlerule*[.5pc]{.}\;\thecontentspage}
[]

\newcounter{data}
\newcounter{result}
\newcounter{pythoncode}
\newcounter{matlab}

\usepackage{xpatch}

\xpretocmd{\chapter}{%
  \addtocontents{data}{\protect\addvspace{10pt}}
  \addtocontents{result}{\protect\addvspace{10pt}}
  \addtocontents{matlab}{\protect\addvspace{10pt}}
}{}{}

\RequirePackage[framemethod=default]{mdframed} 

%======================================================================================
%   HYPERLINKS IN THE DOCUMENTS
%======================================================================================

\usepackage{hyperref}
\hypersetup{colorlinks=true,
            linkcolor = .,
            breaklinks=true,
            urlcolor=blue,
            citecolor = \maincolor,
            bookmarksopen=false,
            pdftitle={Title},
            pdfauthor={Author}}

%========================================================================================
%   Math Formulas
%========================================================================================

\usepackage{empheq}
\usepackage[framemethod=default]{mdframed}
\usepackage{mathtools}
\usepackage[most]{tcolorbox}

\definecolor{light-gray}{gray}{0.92}
\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\tcbuselibrary{skins,breakable,listings}
\newtcbinputlisting[use counter=result,list inside=result,number within=chapter,list type=result]{\inputresult}[3][]{%listing options={style=mystyleresults},%
    list text = {#2},
    enhanced,noparskip,breakable,colback=blue!20,colbacktitle = blue,
    %opacitybacktitle=0,%
    title after break={\centering\footnotesize\itshape\bfseries\strut Result~\theresult~--~continued},%
    listing only,listing options={xleftmargin=-1mm,#1,style=mystyleresults},after upper={\centering\strut{\bfseries Result~\theresult:}~#2},%
    listing file={#3},#1}
\newtcbinputlisting[use counter=matlab,list inside=matlab,number within=chapter,list type=matlab]{\inputmatlabold}[4][]{%
  list text = {#2},  
  enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
  fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimg~#3},%
  title after break={\centering\footnotesize\itshape\strut Matlab Code~\thematlab~--~continued},%
  listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor,basicstyle=\ttfamily\small},list entry=#4,
  after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
  frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
  overlay={\draw[gray,line width=1mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
  %
  listing file={#3},#1
}

\DeclareTCBInputListing[use counter=matlab,list inside=matlab,number within=chapter,list type=matlab]{\inputmatlab}{O{}+m+O{#2}+m}{%
  list text = {#2},
  enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
  fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimg~#4},%
  title after break={\centering\footnotesize\itshape\strut Matlab Code~\thematlab~--~continued},%
  listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor,basicstyle=\ttfamily\small},list entry=#3,
  after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
  frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
  overlay={\draw[gray,line width=1mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
  %
  listing file={#4},#1
}

\lstdefinestyle{mystyleresults}{
    commentstyle=\color{codegreen},
    keywordstyle=\color{black},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{black},
    basicstyle=\scriptsize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=none,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=6
}

\makeatletter
\newcommand{\ttll@data}{-1000}
\newcommand{\ttll@result}{-1000}
\newcommand{\ttll@matlab}{-1000}
\makeatother

%========================================================================================
%   MatLab Code
%========================================================================================

\usepackage[numbered,framed]{matlab-prettifier}

\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}

\hypersetup{linkcolor = blue}

\begin{document}
\lstlistoflistings

\tcblistof[\chapter*]{matlab}{\listofmatlabname}
\tcblistof[\chapter*]{result}{\listofresultname}

\chapter{New Code}
\inputresult{Expansion Algorithm}{sample.m}

\inputmatlab{Expansion Algorithm}[Test Code]{sample.m}
\end{document} 

答案1

问题在于代码list text=中用得太早,没有使用给出\inputmatlab的特殊格式。移至选项末尾即可解决该问题。titletoc\thecontentslabellist text\inputmatlab

\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}

\usepackage{calc}
\usepackage{graphicx, rotating, ltablex, dcolumn, booktabs}
\usepackage{pdflscape}
\usepackage[usenames,dvipsnames,svgnames,table,xcdraw]{xcolor}
\usepackage{tikz}
\usepackage{eso-pic}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}
\usepackage{listings}
\usepackage{xparse}
\usepackage{titletoc}
\usepackage{avant}
\usepackage{microtype}
\usepackage[T1]{fontenc}

\newcommand{\codeimg}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-a}}}$}
\newcommand*{\maincolor}{blue}

%======================================================================================
%   CHAPTER HEADINGS
%======================================================================================

\newcommand{\listofmatlabname}{List of Matlab Code}
\newcommand{\listofresultname}{List of Results}

\usepackage{etoolbox,fancyhdr}

%======================================================================================
%   MAIN TABLE OF CONTENTS
%======================================================================================

\contentsmargin{0cm}

% Part text styling
\titlecontents{part}[0cm]
{\addvspace{20pt}\centering\large\bfseries}
{}
{}
{}

% Chapter text styling
\titlecontents{chapter}[1.25cm] % Indentation
{\addvspace{12pt}\large\sffamily\bfseries} % Spacing and font options for chapters
{\color{\maincolor!60}\contentslabel[\Large\thecontentslabel]{1.25cm}\color{\maincolor}} % Chapter number
{\color{\maincolor}}
{\color{\maincolor!60}\normalsize\;\titlerule*[.5pc]{.}\;\thecontentspage} % Page number

% Section text styling
\titlecontents{section}[1.25cm] % Indentation
{\addvspace{3pt}\sffamily\bfseries} % Spacing and font options for sections
{\contentslabel[\thecontentslabel]{1.25cm}} % Section number
{}
{\hfill\color{black}\thecontentspage} % Page number
[]

% Subsection text styling
\titlecontents{subsection}[1.25cm] % Indentation
{\addvspace{1pt}\sffamily\small} % Spacing and font options for subsections
{\contentslabel[\thecontentslabel]{1.25cm}} % Subsection number
{}
{\ \titlerule*[.5pc]{.}\;\thecontentspage} % Page number
[]

% List of figures
\titlecontents{figure}[0em]
{\addvspace{2pt}\sffamily}
{\thecontentslabel\hspace*{1em}}
{}
{\ \titlerule*[.5pc]{.}\;\thecontentspage}
[]

% List of tables
\titlecontents{table}[0em]
{\addvspace{2pt}\sffamily}
{\thecontentslabel\hspace*{1em}}
{}
{\ \titlerule*[.5pc]{.}\;\thecontentspage}
[]

% List of listings
\contentsuse{lstlisting}{lol}
\titlecontents{lstlisting}[0em]
  {\addvspace{5pt}\sffamily}
  {\thecontentslabel\hspace*{1em}}
  {}
  {\ \titlerule*[.5pc]{.}\;\thecontentspage}
  []

% List of Matlab
\titlecontents{matlab}[0em]
{\addvspace{2pt}\sffamily}
{\thecontentslabel.\hspace*{1em}}
{}
{\ \titlerule*[.5pc]{.}\;\thecontentspage}
[]

% List of results
\titlecontents{result}[0em]
{\addvspace{2pt}\sffamily}
{\thecontentslabel\hspace*{1em}}
{}
{\ \titlerule*[.5pc]{.}\;\thecontentspage}
[]

\newcounter{data}
\newcounter{result}
\newcounter{pythoncode}
\newcounter{matlab}

\usepackage{xpatch}

\xpretocmd{\chapter}{%
  \addtocontents{data}{\protect\addvspace{10pt}}
  \addtocontents{result}{\protect\addvspace{10pt}}
  \addtocontents{matlab}{\protect\addvspace{10pt}}
}{}{}

\RequirePackage[framemethod=default]{mdframed} 

%======================================================================================
%   HYPERLINKS IN THE DOCUMENTS
%======================================================================================


%========================================================================================
%   Math Formulas
%========================================================================================

\usepackage{empheq}
\usepackage[framemethod=default]{mdframed}
\usepackage{mathtools}
\usepackage[most]{tcolorbox}

\definecolor{light-gray}{gray}{0.92}
\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\newtcbinputlisting[use counter=result,list inside=result,number within=chapter,list type=result]{\inputresult}[3][]{%listing options={style=mystyleresults},%
  list text = {#2},
    enhanced,noparskip,breakable,colback=blue!20,colbacktitle = blue,
    %opacitybacktitle=0,%
    title after break={\centering\footnotesize\itshape\bfseries\strut Result~\theresult~--~continued},%
    listing only,listing options={xleftmargin=-1mm,#1,style=mystyleresults},after upper={\centering\strut{\bfseries Result~\theresult:}~#2},%
    listing file={#3},#1}




\DeclareTCBInputListing[
use counter=matlab,
list inside=matlab,
number within=chapter,
list type=matlab
]{\inputmatlab}{O{}+m+O{#2}+m}{%
  enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
  fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimg~#4},%
  title after break={\centering\footnotesize\itshape\strut Matlab Code~\thematlab~--~continued},%
  listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor,basicstyle=\ttfamily\small},list entry=#3,
  after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
  frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
  overlay={\draw[gray,line width=1mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
  %
  listing file={#4},
  list text={#2},
  #1
}

\lstdefinestyle{mystyleresults}{
    commentstyle=\color{codegreen},
    keywordstyle=\color{black},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{black},
    basicstyle=\scriptsize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=none,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=6
}

\makeatletter
\newcommand{\ttll@data}{-1000}
\newcommand{\ttll@result}{-1000}
\newcommand{\ttll@matlab}{-1000}
\makeatother

%========================================================================================
%   MatLab Code
%========================================================================================

\usepackage[numbered,framed]{matlab-prettifier}

\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}

\usepackage{hyperref}

\hypersetup{colorlinks=true,
            linkcolor = .,
            breaklinks=true,
            urlcolor=blue,
            citecolor = \maincolor,
            bookmarksopen=false,
            pdftitle={Title},
            pdfauthor={Author}}


\hypersetup{linkcolor = blue}

\begin{document}
\lstlistoflistings

\tcblistof[\chapter*]{matlab}{\listofmatlabname}
\tcblistof[\chapter*]{result}{\listofresultname}

\chapter{New Code}
\inputresult{Expansion Algorithm}{sample.m}

\inputmatlab{Expansion Algorithm}[Test Code Foostuff]{sample.m}


\end{document} 

在此处输入图片描述

相关内容