Text FormattingPlace * (or other) at section number

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
yiorgosb
Posts: 30
Joined: Mon Aug 02, 2010 11:56 pm

Place * (or other) at section number

Post by yiorgosb »

Some of my sections are optional for reading so I would like to place an *
at the corresponding number of the section title, i.e. something like

*12-5 Double slit experiment

Is there any straightforward way to achieve this or should I override a variable or two?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Place * (or other) at section number

Post by west.logan »

If all you want is the symbol itself, then where ever you want it to appear, you can use:

Code: Select all

\textasteriskcentered
yiorgosb
Posts: 30
Joined: Mon Aug 02, 2010 11:56 pm

Re: Place * (or other) at section number

Post by yiorgosb »

Unfortunately this doesn't work.
As I said I want the symbol (* in my case) to be placed in front of the section number. Like

*12-1 This is a section title
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Re: Place * (or other) at section number

Post by west.logan »

Are you using the article class? Memoir? Do you have an MWE?
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Place * (or other) at section number

Post by west.logan »

Tacky, but this works as a temporary fix.

Code: Select all

\documentclass{article}

\newcommand{\starsection}[1]{\addtocounter{section}{1}%
\addcontentsline{toc}{section}{*\thesection\hspace{1em}#1}%
{\vspace{1.5em}\par\noindent\bfseries\Large *\thesection\hspace{0.5em} #1}\par\vspace{1em}\noindent}

\begin{document}

\tableofcontents
\section{First section}
Some text.
\subsection{subsection}
Text goes here.
\starsection{Second section}
This is text.
\subsection{second subsection}
More text.
\section{Third section}
This is more text.
\subsection{third subsection}
How do you do?
\end{document}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Place * (or other) at section number

Post by localghost »

yiorgosb wrote:Some of my sections are optional for reading so I would like to place an * at the corresponding number of the section title […] Is there any straightforward way to achieve this or should I override a variable or two?
Straightforward, no. Possible, yes. The titlesec/titletoc manual introduces a method that allows to tag the concerned section(s) in the document as well as in the ToC (Section 7.7 — Marking entries with asterisks, p. 22). Due to a missing minimal example I derived an example for the »article« class (because you talked of sections).

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[pagestyles,raggedright]{titlesec}
\usepackage{titletoc}
\usepackage{blindtext}

\title{A document with stuff for advanced reading}
\author{yiorgosb}

\newcommand*{\secmark}{}
\newcommand*{\marktotoc}[1]{\renewcommand{\secmark}{#1}}
\newenvironment{advanced}{
  \renewcommand*{\secmark}{*}%
  \addtocontents{toc}{\protect\marktotoc{*}}
}{\addtocontents{toc}{\protect\marktotoc{}}}

\titleformat{\section}{\normalfont\Large\bfseries}{\makebox[1.5em][l]{\secmark\thesection}}{0.4em}{}
\titlecontents{section}[1.5em]{}{\bfseries\contentslabel[\secmark\thecontentslabel]{1.5em}}{\hspace*{-1.5em}}{\titlerule*{}\bfseries\contentspage}

\newpagestyle{front}{%
  \headrule
  \sethead[\thepage][][\subsectiontitle]{\sectiontitle}{}{\thepage}
  \setfoot[][][]{}{}{}
}

\newpagestyle{main}{%
  \headrule
  \sethead[\thepage][][\thesubsection\quad\subsectiontitle]{\thesection\quad\sectiontitle}{}{\thepage}
  \setfoot[][][]{}{}{}
}
\newpagestyle{back}{%
  \headrule
  \sethead[\thepage][][\thesubsection\quad\subsectiontitle]{\thesection\quad\sectiontitle}{}{\thepage}
  \setfoot[][][]{}{}{}
}


\begin{document}
  \pagestyle{front}
  \maketitle
  \tableofcontents\newpage

  \pagestyle{main}
  \blinddocument
  \blinddocument
  \begin{advanced}
    \blinddocument
  \end{advanced}

  \appendix
  \pagestyle{back}
  \blinddocument
\end{document}
Refer to the manual of the involved packages to learn more. The example contains also some additional features of the two above mentioned packages.

As always, the blindtext package is only for creating dummy text thus not part of the actual solution.


Thorsten
yiorgosb
Posts: 30
Joined: Mon Aug 02, 2010 11:56 pm

Place * (or other) at section number

Post by yiorgosb »

Thank you for your reply.
The suggested solution works like a charm.

However, since my sections are large it is cumbersome to follow
the \begin{advanced}...\end{advanced} environment.
Is it possible to define it as a newcommand?

A note for any future reader:
As I found by trial and error the definition:

Code: Select all

\titleformat{\section}{\normalfont\Large\bfseries}{\makebox[1.5em][l]{\secmark\thesection}}{0.4em}{}
in required for the mark (*) to appear.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Place * (or other) at section number

Post by localghost »

yiorgosb wrote:[…] However, since my sections are large it is cumbersome to follow the \begin{advanced}...\end{advanced} environment. Is it possible to define it as a newcommand? […]
I see two possible solutions.
  • Split your document by swapping the sections to external files with one single section each. Then you could use the \include or \input command inside the new »advanced« environment without losing track of the marked sections.
  • Replace the definition of the »advanced« environment with the definition of two new commands which serve as switches (see code below). They will turn the asterisks on or off. Just put them right before a section wherever you need them.
Here's again a complete example.

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[pagestyles,raggedright]{titlesec}
\usepackage{titletoc}
\usepackage{blindtext}

\title{A document with stuff for advanced reading}
\author{yiorgosb}

\newcommand*{\secmark}{}
\newcommand*{\marktotoc}[1]{\renewcommand{\secmark}{#1}}
\newcommand*{\advanced}{%
  \renewcommand*{\secmark}{*}%
  \addtocontents{toc}{\protect\marktotoc{*}}%
}
\newcommand*{\basic}{%
  \renewcommand*{\secmark}{}%
  \addtocontents{toc}{\protect\marktotoc{}}%
}

\titleformat{\section}{\normalfont\Large\bfseries}{\makebox[1.5em][l]{\secmark\thesection}}{0.4em}{}
\titlecontents{section}[1.5em]{}{\bfseries\contentslabel[\secmark\thecontentslabel]{1.5em}}{\hspace*{-1.5em}}{\titlerule*{}\bfseries\contentspage}

\newpagestyle{front}{%
  \headrule
  \sethead[\thepage][][\subsectiontitle]{\sectiontitle}{}{\thepage}
  \setfoot[][][]{}{}{}
}

\newpagestyle{main}{%
  \headrule
  \sethead[\thepage][][\thesubsection\quad\subsectiontitle]{\thesection\quad\sectiontitle}{}{\thepage}
  \setfoot[][][]{}{}{}
}
\newpagestyle{back}{%
  \headrule
  \sethead[\thepage][][\thesubsection\quad\subsectiontitle]{\thesection\quad\sectiontitle}{}{\thepage}
  \setfoot[][][]{}{}{}
}


\begin{document}
  \pagestyle{front}
  \maketitle
  \tableofcontents\newpage

  \pagestyle{main}
  \blinddocument
  \advanced
  \blinddocument
  \basic
  \blinddocument

  \appendix
  \pagestyle{back}
  \blinddocument
\end{document}
yiorgosb
Posts: 30
Joined: Mon Aug 02, 2010 11:56 pm

Place * (or other) at section number

Post by yiorgosb »

localghost wrote: I see two possible solutions.
Thank your for your reply.

When I was asking for a command what I had in mind was something like
\newcommand{\advsection}{some definitions here}
which would define the new advanced section.
Then the usage of this command to text would be

Code: Select all

    \documentclass[11pt,a4paper]{book}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage{babel}
    \usepackage[pagestyles,raggedright]{titlesec}
    \usepackage{titletoc}
    \usepackage{blindtext}

    \newcommand{\advsection}{some definitions here}

    \begin{document}
 
      \section{Normal section}
      \blindtext

      \advsection{Advanced section}
     \blindtext

    \end{document}
but as I may suppose by your post this is not possible.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Place * (or other) at section number

Post by localghost »

If the stuff I provided is not satisfactory, you should take a look at the code example that west.logan provided earlier. I contributed my share to solve this problem.
Post Reply