7

How to add * to some (i.e. not all) section numbers?

Before

2.1 Differentiation

2.2 Integration

After

2.1 Differentiation

2.2* Integration

  • The documentclass is jlreq.

  • I don’t want to use a package because it can break the style.

  • I need the starred sections to be included in a table of contents.

  • There will be cross-references to some of starred sections. The section number should be displayed in the cross-reference with the asterisk.

Small sample document

% !TEX TS-program = lualatex
\documentclass[book]{jlreq}

\begin{document}

\tableofcontents

\chapter{Calculus}
\section{Differentiation}
\section{Integration}

\end{document}
2
  • 3
    Welcome to TeX.se. The jlreq class is quite non-standard relative to other classes since it's designed for Japanese typesetting. It would be helpful to add a small sample document using the class for people to play with. Do you need the starred sections to be included in a table of contents?
    – Alan Munn
    Aug 1, 2023 at 0:59
  • 3
    Should the asterisk markers be present just in the sectioning headers? Will there be cross-references to some of "starred" sections? If so, how should the section number be displayed in the cross-reference: with or without the asterisk?
    – Mico
    Aug 1, 2023 at 2:44

2 Answers 2

8

You can use a conditional and mark “difficult” sections with

\section+{Title}

This also allows the standard optional argument to \section, so you can still do

\section+[Short title]{Long title}

as usual. A \label can go anywhere: inside the main argument or also outside. Also \section* will work as usual.

\documentclass[oneside]{book}
\usepackage[a6paper,margin=1.5cm]{geometry}
\pagestyle{plain}

\NewCommandCopy{\latexsection}{\section}
\RenewDocumentCommand{\section}{t+sO{#4}m}{%
  \IfBooleanT{#1}{\difficultsectiontrue}
  \IfBooleanTF{#2}{%
    % called as \section*
    \latexsection*{#4}%
  }{%
    \latexsection[#3]{#4}%
  }%
  \difficultsectionfalse
}
\newif\ifdifficultsection
\renewcommand{\thesection}{%
  \thechapter.\arabic{section}%
  \ifdifficultsection *\fi
}
\renewcommand{\thesubsection}{%
  \thechapter.\arabic{section}.\arabic{subsection}%
}

\begin{document}

\tableofcontents

\chapter{Calculus}

We have section~\ref{diff} and section~\ref{int}

\section{Differentiation}\label{diff}
\subsection{Preliminaries}
\section+{Integration}\label{int}
\subsection{Preliminaries}

\end{document}

I also redefined \thesubsection so it won't have a *.

Production note: the first three lines in the above code are only aimed to obtain a smaller picture of the output.

enter image description here

5

Here's a solution which sets up a new command, \starredsection. It meets all four of the OP's requirements. Its limitations are (a) any \label command must be included in the argument of \starredsection and (b) no optional argument is recognized.

The ToC page:

enter image description here

The first main page:

enter image description here

% !TEX TS-program = lualatex
\documentclass[book]{jlreq}

\newcommand\starredsection[1]{%
   \begingroup % localize scope of next instruction
   \renewcommand\thesection{\arabic{chapter}.\arabic{section}*}
   \section{#1}
   \endgroup}

\begin{document}

\tableofcontents

\chapter{Calculus}

\section{Differentiation}
\starredsection{Integration\label{sec:integ}}
\subsection{Hello}
\section{Whatever}\label{sec:what}

\noindent
Cross-references to sections \ref{sec:integ} and \ref{sec:what}.

\end{document}

You must log in to answer this question.

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