0

OBS: I've made a previous post about my problem, but I was not clear enough. Therefore I will try again!

I'm working on a math lecture notes template, and have the following:

\section[Lecture \thesection: <<title>>]{Lecture \thesection}

where \thesection is the lecture number (i.e. section number) and "title" is the section title. Some courses have well over thirty lectures, so it does get very repetitive; not to mention bad programming. I want something like this:

\section{<<title>>}

image

but want it to behave like the former. I have tried to use \renewcommand, changing in the \titleformat and the \let\old redefinition, but nothing have worked so far. Here is my MWE with all section and table of contents commands.

MWE

\documentclass[a4paper,12pt]{article}

% ----- Packages --------------------------------------------

\usepackage{titlesec} % Section formatting
\usepackage[subfigure]{tocloft} % ToC formatting
\usepackage[toc]{multitoc} % ToC formatting


% ----- Section formatting ----------------------------------

\titleformat{\section}
    {\sffamily\Large\bfseries}
    {\!{\fontsize{24}{24}\selectfont\textreferencemark}\;\;}
    {0em}
    {} % Tried: "Lecture \thesection" here. Didn't work!


% ----- ToC formatting --------------------------------------

\setlength{\cftbeforesecskip}{3pt}
\cftsetindents{section}{0em}{0em}
\renewcommand{\cfttoctitlefont}{\sffamily\Large\bfseries}
\renewcommand{\cftsecfont}{\normalfont}
\renewcommand{\cftsecpagefont}{\normalfont}
\renewcommand{\cftsubsecfont}{\normalfont}
\renewcommand{\cftsubsecpagefont}{\normalfont}
\renewcommand{\cftdot}{}

\renewcommand*{\multicolumntoc}{2}
\setlength{\columnseprule}{0.5pt}
\setlength{\columnsep}{1cm}

\makeatletter
    \renewcommand{\cftsecpresnum}{\begin{lrbox}{\@tempboxa}}
    \renewcommand{\cftsecaftersnum}{\end{lrbox}}
\makeatother
-------------------------------------------------------------

\begin{document}

\tableofcontents % ToC

% Beginning of contents
% \section[long-title]{short-title}
\section[Lecture \thesection: First topic]{Lecture \thesection}
Lorem ipsum...

\section[Lecture \thesection: Second topic]{Lecture \thesection}
Lorem ipsum...

\section[Lecture \thesection: Third topic]{Lecture \thesection}
Lorem ipsum...

\end{document}

Thanks in advance :)

3
  • the markup looks very odd how come you are needing to use \thesection in the title of a numbered section??? Commented Nov 5, 2023 at 23:42
  • 1
    oh you are throwing away the section number with lrbox then adding it back by hand, that's basically the cause of the problem Commented Nov 5, 2023 at 23:48
  • Your code does not compile (without error) because of the --… line and option subfigure. Please note, that a minimal working example should not result in errors not related to the problem.
    – cabohah
    Commented Nov 6, 2023 at 8:23

3 Answers 3

3

Instead of using \section explicitly, define a suitable command, here \lecture.

\documentclass[a4paper,12pt]{article}
\usepackage{subcaption}

% ----- Packages --------------------------------------------

\usepackage{titlesec} % Section formatting
\usepackage[subfigure]{tocloft} % ToC formatting
\usepackage[toc]{multitoc} % ToC formatting


% ----- Section formatting ----------------------------------

\titleformat{\section}
    {\sffamily\Large\bfseries}
    {{\fontsize{24.88}{0}\selectfont\textreferencemark}\enspace}
    {0em}
    {Lecture \thesection}


% ----- ToC formatting --------------------------------------

\setlength{\cftbeforesecskip}{3pt}
\cftsetindents{section}{0em}{0em}
\renewcommand{\cfttoctitlefont}{\sffamily\Large\bfseries}
\renewcommand{\cftsecfont}{\normalfont}
\renewcommand{\cftsecpagefont}{\normalfont}
\renewcommand{\cftsubsecfont}{\normalfont}
\renewcommand{\cftsubsecpagefont}{\normalfont}
\renewcommand{\cftdot}{}

\renewcommand*{\multicolumntoc}{2}
\setlength{\columnseprule}{0.5pt}
\setlength{\columnsep}{1cm}

\makeatletter
    \renewcommand{\cftsecpresnum}{\begin{lrbox}{\@tempboxa}}
    \renewcommand{\cftsecaftersnum}{\end{lrbox}}
\makeatother
%-------------------------------------------------------------

\newcommand{\lecture}[1]{%
  \section[Lecture \thesection: #1]{}%
}

\begin{document}

\tableofcontents % ToC

\lecture{First topic}
Lorem ipsum...

\lecture{Second topic}
Lorem ipsum...

\lecture{Third topic}
Lorem ipsum...

\end{document}

enter image description here

1

First of all: It is seldom a good idea to use more than one package to configure the table of contents, list of figures etc. Independent from this, package multitoc can result in empty lists of floats, e.g., if you add a \listoffigures without adding option lof. It also does not support additional list of floats like the one of package listings or generating using packages like newfloat, float or tocbasic. These also will be empty.

In my opinion wishes likes yours are easier to realize when using a class that already supports many of the wanted features. So here an example using scrartcl instead of article:

\documentclass[a4paper,12pt]{scrartcl}

\usepackage{multicol}

% Changinging the output if the section number in \section:
\renewcommand*{\sectionformat}{%
  {\fontsize{24}{24}\selectfont\textreferencemark}\enskip
  Lecture \thesection
}

% Changing the section ToC entries:
\setkomafont{sectionentry}{\normalfont}% Don't use sans-serif bold for the
                                       % section ToC entries.
\newcommand*{\LectureTOCNumFormat}[1]{\let\autodot:Lecture #1}% New definition.
\DeclareTOCStyleEntry[%
  dynnumwidth,% automatic adapt the width reserved for the number
  entrynumberformat=\LectureTOCNumFormat,% use the new definition above for
                                % the number
  beforeskip=0pt,% no vertical space above section entires
]{section}{section}

% Two columns for the ToC:
\BeforeStartingTOC[toc]{\begin{multicols}{2}}% 
\AfterStartingTOC[toc]{\end{multicols}}
\setlength{\columnseprule}{0.5pt}
\setlength{\columnsep}{1cm}

% Redefinition of \section to use the mandatory argument for the ToC (and page
% head with page style headings) only.
\NewCommandCopy{\Section}{\section}
\RenewDocumentCommand{\section}{som}{%
  \IfValueT{#2}{%
    \GenericWarning{}{Optional \string\section\space argument ignored}%
  }%
  \IfBooleanTF{#1}{%
    \Section*{#3}%
  }{%
    \Section[{#3}]{}%
  }%
}

%-------------------------------------------------------------

\begin{document}

\tableofcontents % ToC

% Beginning of contents
% \section[long-title]{short-title}
\section{First topic}
Lorem ipsum...

\section{Second topic}
Lorem ipsum...

\section{Third topic}
Lorem ipsum...

\end{document}

Note: Because of dynnumwidth you need at least three LaTeX runs to get:

enter image description here

Alternatively you can use an explicit width, e.g., numwidth=4.75em.

As you can see, only one additional package is needed: multicol.

1

It is really silly to just eat the section number and then print it yourself by making it part of the title. Why not just use tocloft and titlesec to format things the way you want? (Incidentally, I am always nervous combining more than one toc-formatting packages together; tocloft contains code specifically to work around some things that titlesec does.)

Quick explanation of the code below. List number corresponds to the lines marked in the comments in the code below.

  1. By using [explicit] for titlesec you need to explicitly include #1 in the \titleformat to print the title text. And by not including it, you suppress the section title from being printed at all.
  2. Instead wrapping the the section number by lrbox to eat it, why not just print Lecture before it and : after it, as you are meant to with those commands?
  3. Of course, if you do so, you should reserve enough space for Lecture XX: to print properly. I estimated it at 5em, but you can change it to fit.
  4. And voila, you get to just call \section{<title>} as you wished for in your question.
\documentclass[a4paper,12pt]{article}
\usepackage{subcaption}

% ----- Packages --------------------------------------------

\usepackage[explicit]{titlesec} % Item 1
\usepackage[subfigure]{tocloft} 
\usepackage[toc]{multitoc} 


% ----- Section formatting ----------------------------------

\titleformat{\section}
    {\sffamily\Large\bfseries}
    {{\fontsize{24.88}{0}\selectfont\textreferencemark}\enspace}
    {0em}
    {Lecture \thesection}  % Item 1


% ----- ToC formatting --------------------------------------

\setlength{\cftbeforesecskip}{3pt}
\cftsetindents{section}{0em}{5em}    % Item 3
\renewcommand{\cfttoctitlefont}{\sffamily\Large\bfseries}
\renewcommand{\cftsecfont}{\normalfont}
\renewcommand{\cftsecpagefont}{\normalfont}
\renewcommand{\cftsubsecfont}{\normalfont}
\renewcommand{\cftsubsecpagefont}{\normalfont}
\renewcommand{\cftdot}{}

\renewcommand*{\multicolumntoc}{2}
\setlength{\columnseprule}{0.5pt}
\setlength{\columnsep}{1cm}

\renewcommand{\cftsecpresnum}{Lecture }   % Item 2
\renewcommand{\cftsecaftersnum}{:}        % Item 2
%-------------------------------------------------------------

\begin{document}

\tableofcontents 

\section{First topic}    % Item 4   
Lorem ipsum...

\section{Second topic}
Lorem ipsum...

\section{Third topic}
Lorem ipsum...

\end{document}
1
  • Thanks! It worked like a sharm. Commented Dec 13, 2023 at 22:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .