[Tex/LaTex] Placing symbol in margin

boxesmarginssymbols

I am using

\def\margin_symbol{\rlap{\protect\makebox[-3cm]{X}}}

which I use in sections as \section{\margin_symbol My Header} to place an X 3cm to the left in the margin; however, my problem is that I'm both using it in sections and subsections and therefore the symbols are placed at different horizontally positions.

Can I somehow place the at an absolute position in the margin rather than 3cm to the left as it does now?

Best Answer

This implementation with the tabto package will work with certain limitations. Primarily, when \marginsymbol appears in any sectioning macro argument, it must be the last item in the argument. Second, if you are using a table of contents, or a chapter page-heading, you will need to use the optional argument of the sectioning macro to exclude the margin mark.

\documentclass{book}
\usepackage{tabto,lipsum}
\def\marginsymbol{\protect\marginsymbolhelper}
\def\marginsymbolhelper{\tabto*{-1cm}\makebox[0cm]{$\bullet$}\tabto*{\TabPrevPos}}
\begin{document}
\section[My Header]{My Header\marginsymbol}

\subsection{My subsection Header\marginsymbol}

this is an inline \marginsymbol test. \lipsum[1]

\subsection{My subsubsection Header\marginsymbol}
\end{document}

enter image description here


FOLLOW UP:

The OP asks if it can be done for the table of contents, as well. Here is a way, but it does not address the issue of chapter-page headings that I cited above. So, for the article class, where that is not a direct issue, I provide a revised version of \marginsymbol[<length>] with an optional length argument that is only applied in the toc as an added leftward offset, in order to handle the staggered entries of toc subcategories.

\documentclass{article}
\usepackage{tabto}
\newcommand\marginsymbol[1][0pt]{%
  \tabto*{0cm}\makebox[\dimexpr-1cm-#1\relax][r]{$\bullet$}\tabto*{\TabPrevPos}}
\begin{document}

\tableofcontents
\bigskip
\renewcommand\marginsymbol[1][0pt]{%
  \tabto*{0cm}\makebox[-2cm][c]{$\bullet$}\tabto*{\TabPrevPos}}

\section{My Header\marginsymbol}

\subsection{My subsection Header\marginsymbol[23pt]}

\subsubsection{My subsubsection Header\marginsymbol[55pt]}

\section{My New Header\marginsymbol}

\end{document}

enter image description here

Related Question