如何在目录中居中并对齐标题

如何在目录中居中并对齐标题

我如何将标题居中?我参考了“内容”和“算法索引”,索引表未对齐,算法表应放在更左边

\documentclass{book}

    \usepackage{lipsum}
    \usepackage{listings}
    \usepackage{tocloft}
    \renewcommand{\lstlistingname}{Algoritmo}% Listing -> Algorithm
    \renewcommand{\lstlistlistingname}{Indice de \lstlistingname s}% List of Listings -> List of Algorithms

    \begin{document}

    \tableofcontents
    \lstlistoflistings
    %\listofmyequations
    \chapter{A chapter}
    \section{First section}
    \medskip
    \begin{lstlisting}[language=Java,escapechar=|,caption= Algoritmo  hiperparametros]
    x=a+b
    };
    \end{lstlisting}
    \end{document}

答案1

这使用两种不同的方式来居中标题。 \makebox可能更可靠。

\documentclass{book}

\usepackage{lipsum}
\usepackage{listings}
\usepackage{tocloft}
\usepackage{showframe}% alignment tool, MWE only
\renewcommand{\contentsname}{\makebox[\textwidth]{Contents}}
\renewcommand{\lstlistingname}{Algoritmo}% Listing -> Algorithm
\renewcommand{\lstlistlistingname}{\hfil Indice de \lstlistingname s}% List of Listings -> List of Algorithms
\makeatletter
%\def\l@lstlisting#1#2{\@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}}% original definition
\def\l@lstlisting#1#2{\@dottedtocline{1}{0pt}{2.3em}{#1}{#2}}
\makeatother
\begin{document}

\tableofcontents
\lstlistoflistings
%\listofmyequations
\chapter{A chapter}
\section{First section}
\medskip
\begin{lstlisting}[language=Java,escapechar=|,caption= Algoritmo  hiperparametros]
x=a+b
};
\end{lstlisting}
\end{document}

演示

相关内容