![如何在目录中居中并对齐标题](https://linux22.com/image/386334/%E5%A6%82%E4%BD%95%E5%9C%A8%E7%9B%AE%E5%BD%95%E4%B8%AD%E5%B1%85%E4%B8%AD%E5%B9%B6%E5%AF%B9%E9%BD%90%E6%A0%87%E9%A2%98.png)
我如何将标题居中?我参考了“内容”和“算法索引”,索引表未对齐,算法表应放在更左边
\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}