如何使 \tableofcontents 看起来像表格,如下图所示

如何使 \tableofcontents 看起来像表格,如下图所示

这是所需的输出

在 latex 中,目录没有边框,但我所在的公司要求我遵循这种格式的目录。任何帮助都将不胜感激。

\documentclass[11pt, openany, oneside]{book}
\usepackage[english]{babel}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[paper=a4paper,margin=1.3 in]{geometry}% http://ctan.org/pkg/geometry
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage[toc,page]{appendix}
\newenvironment{myfont}{\fontfamily{phv}\selectfont}{\par} %for setting the font
\usepackage{color}
\definecolor{light-gray}{gray}{0.45} % Just one number between 0 (black) and 1 (white)
\usepackage{titlesec}
\titleformat{\chapter}{\normalfont\large\bfseries\color{black}}{\thechapter.}{1 em}{}
\titleformat{\section}{\normalfont\normalsize\bfseries\color{black}}{\thesection.}{.5 em}{}
\titleformat{\subsection}{\normalfont\small\bfseries\color{black}}{\thesubsection.}{.3 em}{}
\usepackage{tikz}
\usepackage{lastpage}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi}{}{}{}
\makeatother
\titlespacing*{\chapter}
{0 pt}{12 pt}{12 pt}
\usepackage{tabularx}
\renewcommand{\headrulewidth}{0 pt}
\setlength{\headheight}{0 pt}
\pagestyle{fancy}
\fancyhf{}
%\lhead{\textcolor{light-gray}{\large\textbf{\begin{myfont}Company Name Pvt. Ltd.\end{myfont}\textbf{}}}}
\rhead{\includegraphics[width=3.0 cm,keepaspectratio]{../IMAGES/Company_logo.jpg}}
\cfoot{Page \thepage\ of \pageref{LastPage}}
\usepackage[hidelinks]{hyperref}
\usepackage{multirow}
\hypersetup{pdfauthor={XYZ},%
            pdftitle={XYZ},%
            pdfsubject={XYZ},%
            pdfkeywords={XYZ, XYZ}
}

\begin{document}
\fancypagestyle{plain}{}

\chapter{PROJECT SUMMARY}

\section{PROJECT OVERVIEW}
\end{document}

答案1

编辑请参阅下面的改进版本

这是一种黑客行为,将重新定义为命令\chapter,用于写入附加文件。\section\subsubsection\jobname.tabletoc

.tabletoc添加新部分后删除文件,这必须在以后改进,我会修复它。

传统\tableofcontents依然可用。

还应该使用\longtable环境 --> 稍后会调整。肯定还有一些其他问题需要改进。

表格中的行似乎消失了,但这是一个 pdf 查看器问题,至少对于 Linux 上的 Acrobat Reader 而言是这样的。

\documentclass[11pt, openany, oneside]{book}
\usepackage[english]{babel}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[paper=a4paper,margin=1.3 in]{geometry}% http://ctan.org/pkg/geometry
\usepackage[demo]{graphicx}
\usepackage{fancyhdr}
\usepackage[toc,page]{appendix}
\newenvironment{myfont}{\fontfamily{phv}\selectfont}{\par} %for setting the font
\usepackage{color}
\definecolor{light-gray}{gray}{0.45} % Just one number between 0 (black) and 1 (white)
\usepackage{titlesec}
\titleformat{\chapter}{\normalfont\large\bfseries\color{black}}{\thechapter.}{1 em}{}
\titleformat{\section}{\normalfont\normalsize\bfseries\color{black}}{\thesection.}{.5 em}{}
\titleformat{\subsection}{\normalfont\small\bfseries\color{black}}{\thesubsection.}{.3 em}{}
\usepackage{tikz}

\usepackage{lastpage}
\usepackage{etoolbox}
%%% My additions
\usepackage{xcolor}%
\usepackage{xstring}%
\usepackage{colortbl}%

\makeatletter
\patchcmd{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi}{}{}{}
\makeatother
\titlespacing*{\chapter}
{0 pt}{12 pt}{12 pt}
\usepackage{tabularx}
\renewcommand{\headrulewidth}{0 pt}
\setlength{\headheight}{0 pt}
\pagestyle{fancy}
\fancyhf{}
%\lhead{\textcolor{light-gray}{\large\textbf{\begin{myfont}Company Name Pvt. Ltd.\end{myfont}\textbf{}}}}
\rhead{\includegraphics[width=3.0 cm,keepaspectratio]{../IMAGES/Company_logo.jpg}}
\cfoot{Page \thepage\ of \pageref{LastPage}}%
\usepackage{multirow}%

\usepackage[hidelinks]{hyperref}%
\hypersetup{pdfauthor={XYZ},%
            pdftitle={XYZ},%
            pdfsubject={XYZ},%
            pdfkeywords={XYZ, XYZ}
}%

\newwrite\TableTOCFile 


\AtBeginDocument{%
\redefinesectioning{chapter}%
\redefinesectioning{section}%
\redefinesectioning{subsection}%
\redefinesectioning{subsubsection}%

\IfFileExists{\jobname.tabletoc}{}{%
\immediate\openout\TableTOCFile=\jobname.tabletoc%
}%
}%

\AtEndDocument{%
\immediate\closeout\TableTOCFile
}%

\newcommand{\mk}[1]{%

}%

\makeatletter


\let\LaTeXStandardchapter\chapter%
\let\LaTeXStandardsection\section%
\let\LaTeXStandardsubsection\subsection%
\let\LaTeXStandardsubsubsection\subsubsection%



\newcommand{\redefinesectioning}[1]{%

\csgdef{unstarred#1@@noopt}##1{%
\@nameuse{unstarred#1@@opt}[##1]{##1}%
}%

\csgdef{unstarred#1@@opt}[##1]##2{%
\csname LaTeXStandard#1\endcsname[##1]{##2}%
\IfStrEq{#1}{chapter}{%
\immediate\write\TableTOCFile{%\
\csname the#1\endcsname & & & & ##1 & \thepage \tabularnewline^^J
\string\hline%
}%
}{%
  \IfStrEq{#1}{section}{%
    \immediate\write\TableTOCFile{%
      & \csname the#1\endcsname & & & ##1 & \thepage \tabularnewline^^J
      \string\hline%
    }%
  }{%
    \IfStrEq{#1}{subsection}{%
      \immediate\write\TableTOCFile{%
        & & \csname the#1\endcsname & & ##1 & \thepage \tabularnewline^^J
        \string\hline%
      }%
    }{
    \IfStrEq{#1}{subsubsection}{%
      \immediate\write\TableTOCFile{%
        & & & \csname the#1\endcsname  & ##1 & \thepage \tabularnewline^^J
        \string\hline%
      }%
      }{}
    }%
  }}%
}%




\csgdef{starred#1@@noopt}##1{%
\csname LaTeXStandard#1\endcsname*{##1}%
}%

\csgdef{starred#1@@opt}[##1]##2{%
\csname LaTeXStandard#1\endcsname*{##2}%
% Optional argument will be handled here....
}%

\csgdef{starred#1}{%
\@ifnextchar[{\@nameuse{starred#1@@opt}}{\@nameuse{starred#1@@noopt}}%
}%

\csgdef{unstarred#1}{%
\@ifnextchar[{\@nameuse{unstarred#1@@opt}}{\@nameuse{unstarred#1@@noopt}}%
}%

\ifcsdef{#1}{%
\long\csgdef{#1}{%
\@ifstar{\@nameuse{starred#1}}{\@nameuse{unstarred#1}}%
}%
}{}%

}%


\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

\newlength{\tocsectioncolumnwidth}
\setlength{\tocsectioncolumnwidth}{0.08\textwidth}%


\newcommand{\Tableofcontents}{%
\IfFileExists{\jobname.tabletoc}{%
\chapter*{\contentsname}%
\begin{tabular}{|*{4}{p{\tocsectioncolumnwidth}@{}}|p{0.5\textwidth}|c|}
\hline
\multicolumn{4}{|c|}{\cellcolor{lightgray}\textbf{NO.}} & \multicolumn{1}{|c|}{\cellcolor{lightgray}\textbf{CONTENTS}} & \cellcolor{lightgray}\textbf{PAGE} \tabularnewline
\hline
\input{\jobname.tabletoc}%
\end{tabular}
}{}%
\cleardoublepage%
}


\begin{document}

%Traditional TOC
\tableofcontents%

% 'new' TOC
\Tableofcontents


\fancypagestyle{plain}{}


\chapter[other]{First}

\section{First section}%

\chapter[Second short title]{Second short}

\section{Some Section}%


\chapter{Summary}%

\section{Another section}

\subsection{Some subsection}
\subsubsection{Yet another sectioning level}%

\end{document}

笔记后面还会提供更多解释。

在此处输入图片描述

更新

这个版本肯定比第一个发布的版本好。它现在是一个长表,主要适用于从\chapter到 的分段命令\subparagraph,但是,间距中的列间距仍然不正确。目录内容条目现在是超链接。

\documentclass[11pt, openany, oneside]{book}
\usepackage[english]{babel}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[paper=a4paper,margin=1.3 in]{geometry}% http://ctan.org/pkg/geometry
\usepackage[demo]{graphicx}
\usepackage{fancyhdr}
\usepackage[toc,page]{appendix}
\newenvironment{myfont}{\fontfamily{phv}\selectfont}{\par} %for setting the font
\usepackage{color}
\definecolor{light-gray}{gray}{0.45} % Just one number between 0 (black) and 1 (white)
\usepackage{titlesec}
\titleformat{\chapter}{\normalfont\large\bfseries\color{black}}{\thechapter.}{1 em}{}
\titleformat{\section}{\normalfont\normalsize\bfseries\color{black}}{\thesection.}{.5 em}{}
\titleformat{\subsection}{\normalfont\small\bfseries\color{black}}{\thesubsection.}{.3 em}{}


\usepackage{tikz}

\usepackage{lastpage}
\usepackage{etoolbox}
%%% My additions
\usepackage{longtable}%
\usepackage{forloop}%
\usepackage{xcolor}%
\usepackage{xstring}%
\usepackage{colortbl}%

\makeatletter
\patchcmd{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi}{}{}{}
\makeatother
\titlespacing*{\chapter}
{0 pt}{12 pt}{12 pt}
\usepackage{tabularx}
\renewcommand{\headrulewidth}{0 pt}
\setlength{\headheight}{0 pt}
\pagestyle{fancy}
\fancyhf{}
%\lhead{\textcolor{light-gray}{\large\textbf{\begin{myfont}Company Name Pvt. Ltd.\end{myfont}\textbf{}}}}
\rhead{\includegraphics[width=3.0 cm,keepaspectratio]{../IMAGES/Company_logo.jpg}}
\cfoot{Page \thepage\ of \pageref{LastPage}}%
\usepackage{multirow}%

\usepackage[hidelinks]{hyperref}%
%\usepackage{hyperref}%
\hypersetup{pdfauthor={XYZ},%
            pdftitle={XYZ},%
            pdfsubject={XYZ},%
            pdfkeywords={XYZ, XYZ}
}%



\newcommand{\ifnumgreaterequal}[4]{%
  \ifnumgreater{#1}{#2}{#3}{%
    \ifnumequal{#1}{#2}{#3}{#4}}%
}%




\newcounter{NumberOfRuns}%

\newwrite\TableTOCFile

\makeatletter

\newcounter{tabletoc@@tocdepth}%
\setcounter{tocdepth}{4}%


\newcommand{\RedefineSectioningCommands}{%
\listgadd{\SectioningList}{}%
\forcsvlist{\listgadd{\SectioningList}}{part,chapter,section,subsection,subsubsection,paragraph,subparagraph}%
\forlistloop{\letallsectioning}{\SectioningList}%
\forlistloop{\redefinesectioning}{\SectioningList}%
}%


\AtBeginDocument{%
%
\RedefineSectioningCommands%
\setcounter{tabletoc@@tocdepth}{\number\value{tocdepth}}%
\addtocounter{tabletoc@@tocdepth}{1}%

\IfFileExists{\jobname.tabletoc}{}{%
  \immediate\openout\TableTOCFile=\jobname.tabletoc%
}%
}%

\AtEndDocument{%
\refstepcounter{NumberOfRuns}%
\immediate\closeout\TableTOCFile
% Not used so far...
\immediate\write\@auxout{%
\string\setcounter{NumberOfRuns}{\number\value{NumberOfRuns}}
}

}%

\makeatother


\makeatletter


\newcommand{\letallsectioning}[1]{%
\csletcs{LaTeXStandard#1}{#1}%
}%



\newcommand{\ch@@part@@level}{-1}%
\newcommand{\ch@@chapter@@level}{0}%
\newcommand{\ch@@section@@level}{1}%
\newcommand{\ch@@subsection@@level}{2}%
\newcommand{\ch@@subsubsection@@level}{3}%
\newcommand{\ch@@paragraph@@level}{4}%
\newcommand{\ch@@subparagraph@@level}{5}%


\newcounter{loop@@counter}%




\newcommand{\writeatcorrectposition}[2]{%
% 1st arg: counter value
% 2nd arg: section level
% 3rd arg: number of columns
  \forloop{loop@@counter}{0}{\number\value{loop@@counter} < \number\value{tabletoc@@tocdepth}}{%
    \ifnumequal{\number\value{loop@@counter}}{\csname ch@@#2@@level\endcsname}{%
      #1 &}{%
      &}%
     }%
}%


\newcommand{\redefinesectioning}[1]{%

\csgdef{unstarred#1@@noopt}##1{%
\@nameuse{unstarred#1@@opt}[##1]{##1}%
}%

\csgdef{unstarred#1@@opt}[##1]##2{%
\csname LaTeXStandard#1\endcsname[##1]{##2}%
\phantomsection%
\hypertarget{sectionunit::\csname the#1\endcsname}{}%%
\ifblank{##1}{%
% Don't write a toc entry
}{%  
  \ifnumgreaterequal{\csname ch@@#1@@level\endcsname}{\number\value{tabletoc@@tocdepth}}{}{%
    \immediate\write\TableTOCFile{%
      \string\writeatcorrectposition{\csname the#1\endcsname}{#1} 
      \string\hyperlink{sectionunit::\csname the#1\endcsname}{##1} & \thepage \tabularnewline^^J
      \string\hline^^J
    }% End of \immediate\write
  }% End of \ifnumgreaterequal%
}% End of \ifblank
}%

\csgdef{starred#1@@noopt}##1{%
\csname LaTeXStandard#1\endcsname*{##1}%
}%

\csgdef{starred#1@@opt}[##1]##2{%
\csname LaTeXStandard#1\endcsname*{##2}%
% Optional argument will be handled here....
}%

\csgdef{starred#1}{%
\@ifnextchar[{\@nameuse{starred#1@@opt}}{\@nameuse{starred#1@@noopt}}%
}%

\csgdef{unstarred#1}{%
\@ifnextchar[{\@nameuse{unstarred#1@@opt}}{\@nameuse{unstarred#1@@noopt}}%
}%

\ifcsdef{#1}{%
\long\csgdef{#1}{%
\@ifstar{\@nameuse{starred#1}}{\@nameuse{unstarred#1}}%
}%
}{}%

}%


\setcounter{secnumdepth}{3}

\newlength{\tocsectioncolumnwidth}
\setlength{\tocsectioncolumnwidth}{0.08\textwidth}%






\newcommand{\Tableofcontents}{%
\setcounter{tabletoc@@tocdepth}{\number\value{tocdepth}}%
\addtocounter{tabletoc@@tocdepth}{1}%

\IfFileExists{\jobname.tabletoc}{%

\typeout{There are \number\value{tabletoc@@tocdepth} columns}%
\chapter*{\contentsname}%
\begin{longtable}{|*{\number\value{tabletoc@@tocdepth}}{p{\tocsectioncolumnwidth}@{}}|p{0.5\textwidth}|c|}
\hline
\multicolumn{\number\value{tabletoc@@tocdepth}}{|c|}{\cellcolor{light-gray}\textbf{NO.}} & \multicolumn{1}{|c|}{\cellcolor{light-gray}\textbf{CONTENTS}} & \cellcolor{light-gray}\textbf{PAGE} \tabularnewline
\hline
\input{\jobname.tabletoc}%
\end{longtable}
}{}%
\cleardoublepage%
}



\begin{document}

\setcounter{tocdepth}{3}

%Traditional TOC
\tableofcontents%

% 'new' TOC
\Tableofcontents


\fancypagestyle{plain}{}


\chapter[First]{First Chapter}

\section{First section}%

\chapter[Second short title]{Second Chapter}

\section{Some Section}%


\chapter{Summary}%

\section{Another section of Summary and a very long title}

\subsection{Some subsection}
\subsubsection{Yet another sectioning level}%



\chapter{Other Chapter with $\int\limits_{a}^{b}$}%

\end{document}

答案2

这似乎是可行的,至少如果内容适合一页:代码确实很糟糕。

\documentclass[11pt, openany, oneside]{book}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[paper=a4paper,margin=1.3 in]{geometry}% http://ctan.org/pkg/geometry
\usepackage[demo]{graphicx} % demo is just for the example
\usepackage{fancyhdr}
\usepackage[toc,page]{appendix}
\newenvironment{myfont}{\fontfamily{phv}\selectfont}{\par} %for setting the font
\usepackage{titlesec}
\titleformat{\chapter}{\normalfont\large\bfseries\color{black}}{\thechapter.}{1 em}{}
\titleformat{\section}{\normalfont\normalsize\bfseries\color{black}}{\thesection.}{.5 em}{}
\titleformat{\subsection}{\normalfont\small\bfseries\color{black}}{\thesubsection.}{.3 em}{}
\usepackage{tikz}
\usepackage{lastpage}
\usepackage{colortbl}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi}{}{}{}
\makeatother
\titlespacing*{\chapter}
{0 pt}{12 pt}{12 pt}
\usepackage{tabularx}
\renewcommand{\headrulewidth}{0 pt}
\setlength{\headheight}{27pt}
\pagestyle{fancy}
\fancyhf{}
\lhead{\textcolor{light-gray}{\large\bfseries Company Name Pvt. Ltd.}}
%\rhead{\includegraphics[height=1cmwidth=3.0 cm]{../IMAGES/Company_logo.jpg}}
\cfoot{Page \thepage\ of \pageref{LastPage}}
\usepackage[hidelinks]{hyperref}
\usepackage{multirow}
\hypersetup{pdfauthor={XYZ},%
            pdftitle={XYZ},%
            pdfsubject={XYZ},%
            pdfkeywords={XYZ, XYZ}
}

\definecolor{light-gray}{gray}{0.85} % Just one number between 0 (black) and 1 (white)

\fancypagestyle{plain}{}

\begin{document}

\makeatletter
\renewcommand{\contentsline}[1]{\csname l@#1\endcsname}
\renewcommand{\l@chapter}[3]{%
  #1 & \hyperlink{#3}{#2} \\\hline
}
\renewcommand{\l@section}[3]{%
  \quad#1 & \hyperlink{#3}{#2} \\\hline
}
\renewcommand{\numberline}[1]{#1&}
\renewcommand\tableofcontents{%
  \chapter*{\contentsname
      \@mkboth{%
         \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
    \begingroup
    \makeatletter
    \IfFileExists{\jobname.toc}{%
      \noindent
      \begin{tabularx}{\textwidth}{|l|X|c|}
      \hline
      \rowcolor{light-gray}
      \multicolumn{1}{|c|}{NO.} & \multicolumn{1}{c|}{CONTENTS} & PAGE NO. \\\hline
      \@@input \jobname.toc
      \end{tabularx}%
    }{}%
    \if@filesw
      \expandafter\newwrite\csname tf@toc\endcsname
      \immediate\openout \csname tf@toc\endcsname \jobname.toc\relax
    \fi
    \@nobreakfalse
  \endgroup
}
\makeatother

\tableofcontents

\clearpage

\chapter{PROJECT SUMMARY}

\section{PROJECT OVERVIEW}
\section{PROJECT OVERVIEW}

\chapter{PROJECT SUMMARY}

\section{PROJECT OVERVIEW}
\section{PROJECT OVERVIEW}
\end{document}

在此处输入图片描述

相关内容