20

I'm writing a book in LaTeX and want to denote some of the sections, subsections and subsubsections as "harder" (perhaps for the reader to skip on first reading) by placing a diamond in the margin by the section heading. My attempt (below) not surprisingly places the diamond within the section heading, after any number, which I don't like the look of.

\documentclass{article}

\newcommand*{\hardsec}{%
    \mbox{$\diamondsuit$}
}
\setcounter{secnumdepth}{2}

\begin{document}

\section{The first section}
\subsection{Beginnings}
This bit is for all-comers.

\subsection{\hardsec Harder stuff}
Here's some harder stuff

\section{\hardsec The second section}

This entire section is so hard it's in Latin. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

\section{The third section}
\subsection{Middlings}
Bits of this subsection are accessible.
\subsubsection{For everyone}
This part is, for example.
\subsubsection{\hardsec For the (self-)chosen few}
But this part is tricky
\end{document}

How can I (a) move the diamond to the margin (preferably without editing my publisher's .cls file defining styles for the section headings) (b) use a more pleasing, filled diamond symbol (perhaps like the one used to denote the moderators on lists of stackexchange users?)

  • 1
    not tested, but an approach i've often used: \newcommand{\hardsection}{\mbox{\hss$\diamondsuit$\hspace{xxx}}} where xxx is the width of the space occupied by the section number. the \hss effectively zeros out the width of this construct, and shoves the diamond off to the left of the section number. (you will probably want to increase the \hspace amount a bit so the diamond isn't tight up against the section number.) – barbara beeton Feb 2 '15 at 18:58
  • 1
    I always liked the way Dr. Knuth did it in _The TeX Book_—an appropriate model to follow, given that you're using LaTeX! The marks are "dangerous curves" signs, they're in the outer margin, and there can be from zero to three of them. He explains that when you first read the book, you should read only those paragraphs with no such signs - this gives you the structure. On your second read you should probably read most of the "one sign" paragraphs. Then you'll know which of the harder paragraphs will be of interest to you. The code is in the book. – Jamie Hanrahan Feb 3 '15 at 7:13
17

You can define new sectioning commands based on the existing ones, let's say \hardsec, \hardsubsec and \hardsubsubsec

\makeatletter
\newcommand\hardsec{\@startsection {section}{1}{\z@}%
           {-3.5ex \@plus -1ex \@minus -.2ex}%
           {2.3ex \@plus.2ex}%
           {\normalfont\Large\bfseries\noindent\llap{$\diamondsuit$\hspace*{1em}}}}
\newcommand\hardsubsec{\@startsection{subsection}{2}{\z@}%
           {-3.25ex\@plus -1ex \@minus -.2ex}%
           {1.5ex \@plus .2ex}%
           {\normalfont\large\bfseries\noindent\llap{$\diamondsuit$\hspace*{1em}}}}
\newcommand\hardsubsubsec{\@startsection{subsubsection}{3}{\z@}%
           {-3.25ex\@plus -1ex \@minus -.2ex}%
           {1.5ex \@plus .2ex}%
           {\normalfont\normalsize\bfseries\noindent\llap{$\diamondsuit$\hspace*{1em}}}}
\makeatother

and use them instead of the normal sectioning commands when you need them.

MWE:

\documentclass{article}
\usepackage{lipsum}

\makeatletter
\newcommand\hardsec{\@startsection {section}{1}{\z@}%
           {-3.5ex \@plus -1ex \@minus -.2ex}%
           {2.3ex \@plus.2ex}%
           {\normalfont\Large\bfseries\noindent\llap{$\diamondsuit$\hspace*{1em}}}}
\newcommand\hardsubsec{\@startsection{subsection}{2}{\z@}%
           {-3.25ex\@plus -1ex \@minus -.2ex}%
           {1.5ex \@plus .2ex}%
           {\normalfont\large\bfseries\noindent\llap{$\diamondsuit$\hspace*{1em}}}}
\newcommand\hardsubsubsec{\@startsection{subsubsection}{3}{\z@}%
           {-3.25ex\@plus -1ex \@minus -.2ex}%
           {1.5ex \@plus .2ex}%
           {\normalfont\normalsize\bfseries\noindent\llap{$\diamondsuit$\hspace*{1em}}}}
\makeatother

\begin{document}
\section{Not hard}
\lipsum[1]
\hardsec{Hard section}
\lipsum[1]
\hardsubsec{Hard subsection}
\lipsum[1]
\section{Not hard}
\lipsum[1]
\end{document} 

Output

enter image description here

|improve this answer|||||
  • Thank you -- this is the solution I've gone with for my sections, subsections and subsubsections. – xnx Feb 4 '15 at 2:35
  • @xnx you're welcome. Glad to help. – karlkoeller Feb 4 '15 at 6:02
8

I would suggest defining a variant of the traditional \section commands to make the code more readable (rather than adding the markup as part of the section title). It will also allow you to modify things later on if you wish to change the syntax.

enter image description here

\documentclass{article}

\usepackage{xparse,amssymb}

\makeatletter
\NewDocumentCommand{\hardsection}{s o m}{{%
  \renewcommand{\@seccntformat}[1]{\hardsec\csname the##1\endcsname\quad}%
  \IfBooleanTF{#1}
    {% \hardsection*
      \IfNoValueTF{#2}
        {\section*{#3}}% \hardsection*{..}
        {\section*[#2]{#3}}% \hardsection*[..]{..}
    }
    {% \hardsection
      \IfNoValueTF{#2}
        {\section{#3}}% \hardsection{..}
        {\section[#2]{#3}}% \hardsection[..]{..}
    }
}}
\NewDocumentCommand{\hardsubsection}{s o m}{{%
  \renewcommand{\@seccntformat}[1]{\hardsec\csname the##1\endcsname\quad}%
  \IfBooleanTF{#1}
    {% \hardsubsection*
      \IfNoValueTF{#2}
        {\subsection*{#3}}% \hardsubsection*{..}
        {\subsection*[#2]{#3}}% \hardsubsection*[..]{..}
    }
    {% \hardsubsection
      \IfNoValueTF{#2}
        {\subsection{#3}}% \hardsubsection{..}
        {\subsection[#2]{#3}}% \hardsubsection[..]{..}
    }
}}

\newcommand*{\hardsec}{\llap{\hardsecsymbol\hspace{\hardsecspace}}}
\newcommand{\hardsecsymbol}{$\blacklozenge$}
\newlength{\hardsecspace}\setlength{\hardsecspace}{20pt}
\makeatother
\setcounter{secnumdepth}{2}

\begin{document}

\section{The first section}
\subsection{Beginnings}
This bit is for all-comers.

\hardsubsection{Harder stuff}
Here's some harder stuff

\hardsection{The second section}

This entire section is so hard it's in Latin. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

\section{The third section}
\subsection{Middlings}
Bits of this subsection are accessible.
\subsubsection{For everyone}
This part is, for example.
\subsubsection[abc]{\hardsec For the (self-)chosen few}
But this part is tricky
\end{document}

The above definitions of \hardsection (and friends) adjust \@seccntformat - the printing mechanism for the sectional heading numbers. It is assumed that the publisher may change peripheral parts of sectional definitions, but might leave \@seccntformat untouched - essential for making the above suggestion work.

The insertion of \hardsec is font-specific, so you'll see it adjust based on whether you have a \hardsection or a \hardsubsection. This may not be ideal but could be changed. Additionally, instead of using a font-specific spacing \hardsecspace, use a fixed-with space in points, say. Other alignment options also exist, of course.

The xparse use of the definition for \hardsection is to allow you total control over how things are processed. The contents of the new command is grouped, making the redefinition of \@seccntformat temporary.

amssymb provides \blacklozenge which is moderator-like.

|improve this answer|||||
5

A solution with the scalerel and titlesec packages. I define a hard environment that defines a mark, which is empty outside this environment.

\documentclass[[a4paper, x11names]{article}
\usepackage[explicit]{titlesec}
\usepackage{graphicx, xcolor, scalerel}

\usepackage{mathtools}

\titleformat{\section}{\Large\normalfont\bfseries}{\llap{\secmark}\arabic{section}.}{.5em}{#1}

\newcommand{\secmark}{}
\newenvironment{hard}{\renewcommand{\secmark}{%
\stretchrel*{\color{Coral3}\boldsymbol\diamond}{\thesection}\hskip0.333em}
}%a
{}

\begin{document}
  \section{A normal section}
  This section can be skipped since it’s quite easy.

\begin{hard}
  \section{A harder section}
 This one can be skipped on first reading.
 \end{hard}

  \section{Another normal section}
  You can read it if you insist.

\end{document} 

enter image description here

|improve this answer|||||
5

This is a a naïve attempt, but seem to work:

MWE

\documentclass{article}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{marvosym}
\usepackage{xcolor}

\DeclareRobustCommand\Hazard[1][red]{\makebox[0pt][r]{\color{#1}\huge\Biohazard\hspace{2em}}}

\begin{document}
\section{Easy section}
\lipsum[2] 
\section{\Hazard Hazardous section}
\lipsum[2]
\section{\Hazard[orange] Unsafe section}
\lipsum[2]
\section{Soft part}
\lipsum[2]
\end{document} 
|improve this answer|||||
  • Thank you! This is now the way I'm indicating harder examples and questions. – xnx Feb 4 '15 at 2:34
2

You can simply use \everypar for this. For example, \hard will be a "prefix" used as \hard\section{...}, \hard\subsection{...} etc.

\def\hard{\everypar={\llap{$\diamondsuit$\quad}\global\everypar={}}}

You need nothing more.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.