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}

\starSection[2]{xyz}
\starSection{uvw}
\end{document}`.
– Sep 18 '19 at 03:38