1

I newbee to writing a class for syllabus book creation. I am trying to generate a \jobname.toc file outputing commands, which when I try to read through \input{\jobname.toc} inside tabular environment, it doesn't generate any output.

\def\fileversion{1.0}
\def\filedate{2025/08/05}

%% Starting point of class
\NeedsTeXFormat{LaTeX2e}

\ProvidesClass{syllabus}[\filedate\space Version \fileversion]

%% Passing all the previous Options that the rvexamwcmt class revices to article class
\DeclareOption*{%
  \PassOptionsToClass{\CurrentOption}{book}%
}
\ProcessOptions\relax
\LoadClass{book}

\RequirePackage{ifthen}
\RequirePackage{longtable}

% class code
% Open .toc file for writing
\newwrite\syll@tocfile
\immediate\openout\syll@tocfile=\jobname.toctree

\newcommand*{\tocentry}[2]{#1 & #2\\}

\newcommand*{\addtotableofcontents}[1]{%
  \edef\@currenttitle{#1}%
    \immediate\write\syll@tocfile{\string\tocentry\space{\@currenttitle}{\thepage}}%
}

\newcommand\Mytableofcontents{%
    \section*{Contents}
    \IfFileExists{\jobname.toctree}{\jobname.toctree File Found }{\textit{(No entries found.)}}\\
    \begin{table}[h]
    \centering
    \begin{tabular}{|p{1.2\linewidth}|r|}
        \tocentry{Introduction}{2}
        \input{\jobname.toctree}
        \message{>>> Finished reading \jobname.toctree^^J}
    \end{tabular}
    \end{table}
}

\endinput

and Main.tex file:

\documentclass{syllabus}
\usepackage{lipsum} % For dummy text

\begin{document}
    
\Mytableofcontents

\clearpage
\chapter{Introduction}
\section{Introduction}
\addtotableofcontents{Induction}
\lipsum[1]

\section{Methodology}
\addtotableofcontents{Methodology}
\lipsum[2]

\section{Results}
\addtotableofcontents{Results}
\lipsum[3]

\section{Conclusion}
\addtotableofcontents{Conclusion}
\lipsum[4]

\end{document}

I am not getting any output because of \input{}. I also tried modifying the .cls file

\newread\foo
\newcommand\Mytableofcontents{%

        \openin\foo=\jobname.toctree
        \read\foo to \x
        \closein\foo

    \section*{Contents}
    \IfFileExists{\jobname.toctree}{\jobname.toctree File Found }{\textit{(No entries found.)}}\\
    % \hline\\
    \begin{table}[h]
    \centering
    \begin{tabular}{|p{1.2\linewidth}|r|}
        \tocentry{Introduction}{2}
        % \input{\jobname.toctree}
        \x
        \message{>>> Finished reading \jobname.toctree^^J}
    \end{tabular}
    \end{table}
}

Still nothing happens. I am attaching the output here, enter image description here

3
  • you are getting no output because you try to use the same file at the same time for writing and reading. Open it for writing after you have read it in. Commented Aug 6, 2025 at 18:15
  • also even if you use a tabular layout you should never use \begin{table}[h] here it makes no sense to float that table. Commented Aug 6, 2025 at 18:16
  • any particular reason you don't want to use the .toc file, which would handle this automatically? you can write and format the toc however you need, so I'd only add another output stream if you actually need something distinct. otherwise, you're just wasting one of a limited number of such streams. Commented Aug 6, 2025 at 19:12

1 Answer 1

1

It seems you want to get the titles in first column and page numbers in second column. You can achieve this with etoc package, I believe as long as the document class creates a \jobname.toc file consisting of \contentsline lines.

\documentclass{report}
\usepackage{lipsum} % For dummy text

\usepackage{etoc}
\begin{document}

\etocglobaldefs

\etocsetstyle{chapter}
  {}
  {\etociffirst{}{\\\hline}}
  {\etocname &\etocpage}
  {}
\etocsetstyle{section}
  {}
  {\etociffirst{\\\hline}{\\}}
  {\etocname &\etocpage}
  {}
\etocsettocstyle{\begin{table}[htbp]
    \centering
    \begin{tabular}{|p{0.8\linewidth}|r|}
    \hline
    }%
    {\\\hline
     \end{tabular}\caption{THIS IS MY TABLE OF CONTENTS}\end{table}}
\etocsetnexttocdepth {section}
\tableofcontents

\chapter{FIRST CHAP}

\section{Introduction}
\lipsum[1]

\section{Methodology}
\lipsum[2]

\section{Results}
\lipsum[3]

\section{Conclusion}
\lipsum[4]

\chapter{SECOND CHAP}

\section{A}
\lipsum[5]

\section{B}
\lipsum[6]

\end{document}

etoc table TOC

I got inspiration from etoc manual 39. The TOC as a (long) table which has a much more complicated example. I had lots of difficulties with misplaced \noalign. It seems the advices at bottom of first page of that etoc manual section have to be followed.

By the way, your mwe had a p{1.2\linewidth which I did not understand and I used 0.8\linewidth.

3
  • It's not just page number I want to export and I need to export bit complex data which I wish to bring it on to table. So as a trial I tried exporting the section heading. Is there any way to make \input command to work Commented Aug 7, 2025 at 1:26
  • Have you tried with \expandableinput which was added to LaTeX kernel recently? Commented Aug 7, 2025 at 10:30
  • I think you can use etoc for tasks unrelated to section headings, but I am not knowledgeable enough. About using. \jobname.toc some packages like biblatex put things there. So for complex data not really related to sectioning, at first sight using some other extension than .toc looks a priori better to me. The simplest would be for all the table structure \begin{longtable} etc... to be exported to that \jobname.mystuff thing. Then a regular \input should work, and not cause the problems of trying to use if from inside a tabular structure. Commented Aug 7, 2025 at 10:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.