4

I would like to have several kinds of \sections. \starSection{Title} would print “1.3 Title” but \starSection[2]{Title} would print “1.3** Title”.

Could you help me?

Thanks

edit

Actually, I would like to have to such counters. One that produces 1.3\,** Title and another one that produces 1.3\,$^{\displaystyle\textbf{!!}}$.

\gdef\mystars{}
\gdef\myexclmarks{}
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}\mystars\myexclmarks\setImportance{0}\setStars{0}}

\newcounter{secstars} \newcommand\setStars[1]{% \gdef\mystars{}% \ifnum#1=0% \else \setcounter{secstars}{0}% \loop\edef\mystars{*\mystars}% \stepcounter{secstars}% \ifnum\value{secstars}<#1\repeat \edef\mystars{,\mystars} \fi% }

\newcounter{secexclmarks} \newcommand\setImportance[1]{% \gdef\myexclmarks{}% \ifnum#1=0% \else \def\myexclmarksaux{}% \setcounter{secexclmarks}{0}% \loop\edef\myexclmarksaux{!\myexclmarksaux}% \stepcounter{secexclmarks}% \ifnum\value{secexclmarks}<#1\repeat \gdef\myexclmarks{,$^{\displaystyle\textbf{\myexclmarksaux}}$} \fi% }

Colas
  • 6,772
  • 4
  • 46
  • 96
  • 2
    I strongly advise not to do that but the following should do what you want: `\documentclass{article} \newcounter{secstars} \newcommand\starSection[2][0]{% \def\mystars{}% \ifnum#1=0% \else \setcounter{secstars}{0}% \loop\edef\mystars{*\mystars}% \stepcounter{secstars}% \ifnum\value{secstars}<#1\repeat \fi% \renewcommand{\thesection}{\arabic{section}\mystars}% \section{#2}} \begin{document} \starSection[3]{abc}

    \starSection[2]{xyz}

    \starSection{uvw}

    \end{document}`.

    –  Sep 18 '19 at 03:38
  • Thanks Can you make it an answer? – Colas Sep 18 '19 at 15:28

3 Answers3

2

Please note that the original \section command does have an optional argument (which determines what the entry will look like in the table of contents). Assuming that you have decided not to use this, you could do

\documentclass{article}
\newcounter{secstars}
\newcommand\starSection[2][0]{%
\begingroup\def\mystars{}%
\ifnum#1=0%
\else
\setcounter{secstars}{0}%
\loop\edef\mystars{*\mystars}%
\stepcounter{secstars}%
\ifnum\value{secstars}<#1\repeat
\fi%
\renewcommand{\thesection}{\arabic{section}\mystars}%
\section{#2}%
\endgroup}
\begin{document}
\starSection[3]{abc}

\starSection[2]{xyz}

\section{test}

\starSection{uvw}
\end{document}

If you cannot exclude that you will use the optional entry, I would suggest the following somewhat more versatile alternative.

\documentclass{article}
\def\mystars{}%
\renewcommand{\thesection}{\arabic{section}\mystars}%
\newcounter{secstars}
\newcommand\setstars[1]{%
\def\mystars{}%
\ifnum#1=0%
\else
\setcounter{secstars}{0}%
\loop\xdef\mystars{*\mystars}%
\stepcounter{secstars}%
\ifnum\value{secstars}<#1\repeat
\fi%
}
\begin{document}
\setstars{3}
\section{abc}

\setstars{2}
\section[$xyz$]{\boldmath$xyz$\unboldmath}

\setstars{0}
\section{test}

\end{document}

enter image description here

This all assumes that you do not want to make use of packages like titlesec, which offer more advanced options.

2

Note: answer reworked after further information in the question


I suggest to use different names for the different types of section; with the help of \@seccntformat we can append what we want to the section number.

\documentclass{article}
\usepackage{xparse}

\let\latexsection\section

\ExplSyntaxOn

\NewDocumentCommand{\genericsection}{m m D(){0} s O{#6} m}
 {% #1 = formatting command
  % #2 = symbol
  % #3 = rep
  % #4 = * variant
  % #5 = optional short title (default #6)
  % #6 = title
  \tl_set:Nx \l_colas_symbol_tl { \prg_replicate:nn { #3 } { #2 } }
  \tl_if_empty:NTF \l_colas_symbol_tl
   {
    \cs_set_protected:cpn { section@append } { }
   }
   {
    \cs_set_protected:cpn { section@append } { #1{\l_colas_symbol_tl} }
   }
  \IfBooleanTF{#4}
   {
    \latexsection*{#5}
   }
   {
    \latexsection[#5]{#6}
   }
 }
\tl_new:N \l_colas_symbol_tl
\ExplSyntaxOff

\makeatletter
\renewcommand{\@seccntformat}[1]{%
  \csname the#1\endcsname\csname #1@append\endcsname\quad
}
\makeatother

\newcommand{\dsection}{\genericsection{}{*}}
\newcommand{\isection}{\genericsection{\textsuperscript}{!}}
\renewcommand{\section}{\genericsection{}{}}

\begin{document}

\tableofcontents

\section*{Starred section}

\section{Normal section}

\dsection(1){Difficult section}

\dsection(2){Very hard section}

\isection(2){Very important section}

\isection(4){Very very important section}

\section{Another normal section}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Thanks egreg. The answer of Schrodinger is better since p, actually, I have three types of sections. The normals ones, the ** ones and the !! ones. – Colas Sep 19 '19 at 21:49
  • 1
    @Colas Uh! Where's the problem? No “automatic” count, but you won't be using so may symbols after the number, I guess. See edit. – egreg Sep 19 '19 at 21:56
  • Yes, you are right the stars are , * or *** and the exclamation marks are !, !!, !!!, !!!! : resp. difficult sections and important sections. But, the !!! are in exposant. See my edit. Thanks – Colas Sep 22 '19 at 13:26
  • @Colas See edit – egreg Sep 22 '19 at 13:59
  • Thanks but it seems you missed one important point of my message. The important sections are formatted differently: 1.3 \,$^{\displaystyle\textbf{!!!}}$ Title – Colas Sep 22 '19 at 14:17
  • @Colas Just to get full info: do you use cross references? If so, what would be a cross reference to a difficult or important section? – egreg Sep 22 '19 at 14:24
  • The usual number is the reference. For instance, the reference to 1.3*** Title is just 1.3. By the way, if the *** and the !!! don't appear in the table of contents, it is not important. – Colas Sep 22 '19 at 14:41
  • 1
    @Colas OK, now it should be what you need. – egreg Sep 22 '19 at 15:13
0

The question arises in which locations you wish these exclamation-marks and/or stars to occur:

  • In the section-headings in the main text? (Seems to be obvious.)
  • In the repeated section-headings in the table of contents?
  • In the page headings in case of using \pagestyle{headings}?
  • When referencing the corresponding sectioning-number via \ref?
  • When referencing the corresponding sectioning-number via \autoref?
  • In the repeated section-headings listed in the bookmarks-window/contents-window of your pdf-viewing-program?
  • ...

A starting-point where these exclamation-marks and/or stars occur only in the section-headings in the main text could be a "mechanism" where a hook for appending things to the result of \thesection is added to the \thesection-macro. That hook usually is empty but can temporarily be redefined to deliver whatsoever amount of stars and/or exclamation-marks. When \thesection is carried out while the redefinition is in effect, then you get these stars/exclamation-marks.
In the example below the hook is formed by the macro \AppendToNumber,

You can also have macros which take \section-commands as their arguments and which do the needed temporary redefinitions of the hook-macro for you.
In the example below these are the macros \difficult and \important.

In the example below I used David Kastrup's \replicate-macro for inventing an expansion-based macro for producing an amount of stars/exclamation-marks without using/wasting a counter/\count-register and without the need of plenty of temporary macro-assignments.

I give no warranties/guarantees.

I can't tell which additional packages might break this.

\documentclass{article}
\usepackage{hyperref}
\begingroup
\makeatletter
\@firstofone{%
  \endgroup
  %------------------------------------------------------------------------------
  % David Kastrup's replicate, see
  % http://www.gust.org.pl/projects/pearls/2005p/david-kastrup/bachotex2005-david-kastrup-pearl3.pdf
  %   \replicate{<number>}{<tokens>}
  %   e.g., \replicate{3}{XYZ}  -> XYZXYZXYZ
  %------------------------------------------------------------------------------
  \newcommand\xii[2]{\if#2m#1\expandafter\xii\else\expandafter\@gobble\fi{#1}}
  \@ifdefinable\xiii{\long\def\xiii#1\relax#2{\xii{#2}#1\relax}}
  \newcommand\replicate[1]{\expandafter\xiii\romannumeral\number\number#1 000\relax}
  %------------------------------------------------------------------------------
  % Print an amount of stars:
  %------------------------------------------------------------------------------
  \@ifdefinable\StarsLoop{%
    \DeclareRobustCommand\StarsLoop[1]{%
      \ifnum#1=0 \expandafter\@gobble\else\expandafter\@firstofone\fi
      {%
        \@ifundefined{texorpdfstring}{\@firstoftwo}{\texorpdfstring}%
        {\,}{ }%
        \replicate{#1}{*}%
      }%
    }%
  }%
  %------------------------------------------------------------------------------
  % Print an amount of Exclamation-marks:
  %------------------------------------------------------------------------------
  \@ifdefinable\ExclamationMarksLoop{%
    \DeclareRobustCommand\ExclamationMarksLoop[1]{%
      \ifnum#1=0 \expandafter\@gobble\else\expandafter\@firstofone\fi
      {%
        \@ifundefined{texorpdfstring}{\@firstoftwo}{\texorpdfstring}%
        {\,\textsuperscript}{ \@firstofone}{\replicate{#1}{!}}%
      }%
    }%
  }%
  %------------------------------------------------------------------------------
  % Add a hook \AppendToNumber to \thesection for appending tokens to a section number:
  %------------------------------------------------------------------------------
  \newcommand\AppendToNumber{}%
  \renewcommand\thesection{\arabic{section}\noexpand\AppendToNumber}%
    % Usage of \noexpand instead of \protect causes stars/exclamation-marks to
    % end up in the data for referencing-labels also.
  %------------------------------------------------------------------------------
  % \difficult and \important:
  %------------------------------------------------------------------------------
  \newcommand\difficult[2]{%
     \expandafter\def\expandafter\AppendToNumber
     \expandafter{\AppendToNumber\StarsLoop{#1}}%
     #2%
     \let\AppendToNumber=\@empty
  }%
  \newcommand\important[2]{%
     \expandafter\def\expandafter\AppendToNumber
     \expandafter{\AppendToNumber\ExclamationMarksLoop{#1}}%
     #2%
     \let\AppendToNumber=\@empty
  }%
}%-that closes \@firstofone's argument.

\pagestyle{headings}

\begin{document}

\tableofcontents

\newpage

\section*{Starred section}

\section{Normal section}

\difficult{1}{\section{Difficult section}}

\newpage

\difficult{8}{%
  \section{A nicely challenging section}\label{InsideDifficult}%
}\label{OutsideDifficult}

\important{2}{\section{Very important section}}

\important{4}{\section{Very very important section}}

\difficult{2}{\important{2}{\section{Very very difficult and very very important section}}}

\section{Another normal section}

\vfill\noindent\textbf{Referencing:}\bigskip

\noindent You can choose whether to have the referenced number with/without stars/exclamation-marks
by placing the corresponding referencing-label inside/behind the second argument of
\verb|\difficult|/\verb|\important|.\bigskip

\noindent\verb|\ref{InsideDifficult}|: \ref{InsideDifficult}

\noindent\verb|\ref{OutsideDifficult}|: \ref{OutsideDifficult}

\noindent\verb|\autoref{InsideDifficult}|: \autoref{InsideDifficult}

\noindent\verb|\autoref{OutsideDifficult}|: \autoref{OutsideDifficult}

\vfill

\end{document}

enter image description here enter image description here enter image description here

Ulrich Diez
  • 28,770