4

I have a document and I want to customise Contents of it, so I don't want to use \tableofcontents, instead of that I just want to get number of pages of sections and subsections and put them in to the table, so instead of page 1 ...page 7 should be real page number. How possible to do that?

\documentclass{article}       
\usepackage[utf8]{inputenc}   
\usepackage[russian]{babel}   
\usepackage{tabularx}   
\begin{document}       
\begin{tabularx}{\textwidth}{|X|X|X|}   
\hline    
 Разделы & & Страницы \\ \hline   
 Номер & Название & \\ \hline   
 1. & Отрасль применения & page 1 \\ \hline   
 2. & Ответственные лица & page 2 \\ \hline   
 3. & Нормативная база & page 3 \\ \hline   
 4. & Материальная база & page 4 \\ \hline   
 5. & Определения и сокращения & page 5 \\ \hline  
 6. & Основная часть & page 6  \\ \hline   
 7. & Конечные выводы, рекомендации & page 7 \\ \hline    
\section{\large \textbf{\textsc{Отрасль применения}}}    
\section{\large \textbf{\textsc{Ответственные лица}}}    
\section{\large \textbf{\textsc{Нормативная база}}}    
\section{\large \textbf{\textsc{Материальная база}}}    
\subsection{\large \textbf{\textsc{Основные средства}}}    
\subsection{\large \textbf{\textsc{Химические средства}}}   
\section{\large \textbf{\textsc{Определения и сокращения}}}   
\section{\large \textbf{\textsc{Основная часть}}}   
\section{\large \textbf{\textsc{Конечные выводы / рекомендации}}}      
\end{tabularx}   
\end{document}
Lucky_girl
  • 1,365
  • 1
    This is a non-recommendable approach -- You're misusing \section and have to add the section name twice, which is error-prone –  Feb 04 '16 at 14:01
  • 1
    I think you'd be better off using a package such as tocloft to customize the appearance of the Table of Contents. – Mico Feb 04 '16 at 14:04
  • See perhaps my answer to this question here, where I've done such a tabular approach: http://tex.stackexchange.com/questions/263979/redefining-listoffigures-and-listoftables-as-tables, by redefining \section –  Feb 04 '16 at 14:04
  • Sorry, I forgot: That answer is for \listoffigures etc. but should be adaptable to the ToC –  Feb 04 '16 at 14:11
  • Are you familiar with the \pageref macro? – Mico Feb 04 '16 at 14:13
  • etoc can do this, I believe –  Feb 04 '16 at 14:14
  • @Mico No, I don't know nothing about it, will search in the internet. – Lucky_girl Feb 04 '16 at 14:20

2 Answers2

5

If I understand your objective correctly, it may be achieved by using \pageref instructions to point to the page numbers where various \section instructions occur.

enter image description here

\documentclass{article}       
\usepackage[utf8]{inputenc}   
\usepackage[russian]{babel}   
\usepackage{tabularx}   

% fonts for sectioning headers
\usepackage{sectsty}
\allsectionsfont{\large\bfseries\scshape} 

\begin{document}       
\noindent
\begin{tabularx}{\textwidth}{|l|X|r|}   
\hline    
 Разделы &          & Page \\ \hline   
 Номер   & Название &      \\ \hline   
 1. & Отрасль применения & \pageref{sec:1} \\ \hline   
 2. & Ответственные лица & \pageref{sec:2} \\ \hline   
 3. & Нормативная база   & \pageref{sec:3} \\ \hline   
 4. & Материальная база  & \pageref{sec:4} \\ \hline   
 5. & Определения и сокращения & \pageref{sec:5} \\ \hline  
 6. & Основная часть & \pageref{sec:6} \\ \hline   
 7. & Конечные выводы, рекомендации & \pageref{sec:7} \\ \hline  
\end{tabularx}   

% "\clearpage" instructions used only to make sure that the section-level headers are on distinct pages
\clearpage\section{Отрасль применения} \label{sec:1}
\clearpage\section{Ответственные лица} \label{sec:2}
\clearpage\section{Нормативная база}   \label{sec:3}
\clearpage\section{Материальная база}  \label{sec:4}
   \subsection{Основные средства}   
   \subsection{Химические средства}  
\clearpage\section{Определения и сокращения}\label{sec:5}
\clearpage\section{Основная часть}\label{sec:6}
\clearpage\section{Конечные выводы\slash рекомендации}\label{sec:7}
\end{document}
Mico
  • 506,678
  • Sorry, I did not meant to steal the tick! –  Feb 04 '16 at 15:34
  • @ChristianHupfer - That's OK. :-) I honestly have no idea if I understood the OP's objective(s). – Mico Feb 04 '16 at 15:38
  • Mico: You're solution is good of course too (and honestly, I would use a table for ToC for myself) –  Feb 04 '16 at 15:51
4

Now it's working -- with automatic section title setting and hyperlinks, if needed.

\documentclass{article}
\usepackage[lmargin=1.5cm,rmargin=1.5cm]{geometry}

\usepackage{blindtext}
\usepackage{array}
\usepackage{longtable}
\usepackage{tabularx}
\usepackage{xpatch}
\usepackage{xparse}

\newif\ifhyperrefloaded

\usepackage[hypertexnames=true]{hyperref}

\makeatletter
\@ifpackageloaded{hyperref}{\hyperrefloadedtrue}{\hyperrefloadedfalse}

\newcommand{\PhantomSection}{%
  \ifhyperrefloaded
  \phantomsection%
  \fi
}


\def\@startmytoc#1#2{%
  \begingroup
  \makeatletter
  \renewcommand{\arraystretch}{1.5}%
  \centering

  % Table format may be changed%%%
  \begin{tabularx}{0.5\linewidth}{|p{1cm}|X|r|}%
    \multicolumn{3}{c}{\Large \bfseries #2}
    \tabularnewline
    \multicolumn{3}{c}{}\tabularnewline[0.5ex]
    \hline
    \@input{\jobname.#1}%  Input the ToF or ToT file 
  \end{tabularx}%
  \if@filesw%
  \expandafter\newwrite\csname tf@#1\endcsname%
  \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax%
  \fi%

  \endgroup
}

\newcommand{\mytableofcontents}{%
  \cleardoublepage
  \PhantomSection%
  \@startmytoc{tocm}{\contentsname}%
}%


\def\@sect#1#2#3#4#5#6[#7]#8{%
  \def\@@tempa@@{#1}%
  \def\@@tempa@section{section}%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \ifx\@@tempa@@\@@tempa@section
    \addtocontents{tocm}{%
      \protect\writemytocline{\thesection}{#7}{\thepage}% 
    }%
    \fi
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
      \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
        \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \ifx\@@tempa@@\@@tempa@section
      \addtocontents{tocm}{%
        \protect\writemytocline{\thesection}{#7}{\thepage}% 
      }%
      \fi
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
          \fi
        #7}}%
  \fi
  \@xsect{#5}}


\newcommand{\writemytocline}[3]{%
  #1 & #2 &
  \ifhyperrefloaded 
  \protect\hyperlink{section.#1}{#3}%
  \else
  #3%
  \fi
  \tabularnewline
  \hline
}


\makeatother

\begin{document}
\mytableofcontents

\section{A section}
\subsection{A subsection}
\blindtext[10]
\section{Another section}
\blindtext[10]
\section[Short title]{Yet another section}

\end{document}

The Russian version

\documentclass{article}
\usepackage[lmargin=1.5cm,rmargin=1.5cm]{geometry}

\usepackage[utf8]{inputenc}   
\usepackage[russian]{babel}   

\usepackage{blindtext}
\usepackage{array}
\usepackage{tabularx}
\usepackage{xpatch}
\usepackage{xparse}



\newif\ifhyperrefloaded

\usepackage[hypertexnames=true]{hyperref}

\makeatletter
\@ifpackageloaded{hyperref}{\hyperrefloadedtrue}{\hyperrefloadedfalse}

\newcommand{\PhantomSection}{%
  \ifhyperrefloaded
  \phantomsection%
  \fi
}


\def\@startmytoc#1#2{%
  \begingroup
  \makeatletter
  \renewcommand{\arraystretch}{1.5}%
  \centering

  % Table format may be changed%%%
  \begin{tabularx}{\linewidth}{|p{2cm}|X|r|}%
    \hline
    Разделы & & Страницы \tabularnewline 
    \hline   
    Номер & Название & \tabularnewline
    \hline   
    \@input{\jobname.#1}%  Input the ToF or ToT file 
  \end{tabularx}%
  \if@filesw%
  \expandafter\newwrite\csname tf@#1\endcsname%
  \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax%
  \fi%

  \endgroup
}

\newcommand{\mytableofcontents}{%
  \cleardoublepage
  \PhantomSection%
  \@startmytoc{tocm}{\contentsname}%
}%


\def\@sect#1#2#3#4#5#6[#7]#8{%
  \def\@@tempa@@{#1}%
  \def\@@tempa@section{section}%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \ifx\@@tempa@@\@@tempa@section
    \addtocontents{tocm}{%
      \protect\writemytocline{\thesection}{#7}{\thepage}% 
    }%
    \fi
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
      \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
        \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \ifx\@@tempa@@\@@tempa@section
      \addtocontents{tocm}{%
        \protect\writemytocline{\thesection}{#7}{\thepage}% 
      }%
      \fi
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
          \fi
        #7}}%
  \fi
  \@xsect{#5}}


\newcommand{\writemytocline}[3]{%
  #1 & #2 &
  \ifhyperrefloaded 
  \protect\hyperlink{section.#1}{#3}%
  \else
  #3%
  \fi
  \tabularnewline
  \hline
}


\makeatother

\begin{document}
\mytableofcontents


\section{\large \textbf{\textsc{Отрасль применения}}}    
\blindtext[10]
\section{\large \textbf{\textsc{Ответственные лица}}}    
\blindtext[10]
\section{\large \textbf{\textsc{Нормативная база}}}    
\blindtext[10]
\section{\large \textbf{\textsc{Материальная база}}}    
\blindtext[10]
\subsection{\large \textbf{\textsc{Основные средства}}}    
\blindtext[10]
\subsection{\large \textbf{\textsc{Химические средства}}}   
\blindtext[10]
\section{\large \textbf{\textsc{Определения и сокращения}}}   
\blindtext[10]
\section{\large \textbf{\textsc{Основная часть}}}   
\blindtext[10]
\section{\large \textbf{\textsc{Конечные выводы / рекомендации}}}      

\end{document}

enter image description here

  • @Lucky_girl: I've added the Russian version as well. Please note that tabularx does not break the page if there too much entries (i.e. to much sections!) –  Feb 04 '16 at 15:19