2

Recently I came along this awesome post about cool text highlighting in LaTeX and the corresponding answer from @Antal Spector-Zabusky. I do want to implement this highlighting into my lecture notes. In particular I have multiple environments defined (like definition, remark and proposition) (using mdframed and amsthm).

Here is a minimal example

\documentclass{report}

\usepackage{amsthm}
\usepackage{xcolor}
\usepackage{mdframed}

\definecolor{superlight}{HTML}{F5F5F5}
\mdfdefinestyle{basic-gray-box}{backgroundcolor=superlight}

\setlength{\parindent}{0pt}

\newtheoremstyle{mystyle}
  {}
  {}
  {\itshape}
  {}
  {\bfseries}
  {.}
  { }
  {\thmname{\highlight{#1}}\thmnumber{ #2}\thmnote{ (#3)}}%  

\theoremstyle{mystyle}
\newmdtheoremenv[style=basic-gray-box]{proposition}{Satz}[chapter]

% Highlight
\usepackage{soul}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathmorphing}

\makeatletter

\newcommand{\defhighlighter}[3][]{%
  \tikzset{every highlighter/.style={color=#2, fill opacity=#3, #1}}%
}

\defhighlighter{yellow}{.5}

\newcommand{\highlight@DoHighlight}{
  \fill [ decoration = {random steps, amplitude=2pt, segment length=15pt}
        , outer sep = -30pt, inner sep = 0pt, decorate
        , every highlighter, this highlighter ]
        ($(begin highlight)+(0,8pt)$) rectangle ($(end highlight)+(0,-3pt)$) ;
}

\newcommand{\highlight@BeginHighlight}{
  \coordinate (begin highlight) at (0,0) ;
}

\newcommand{\highlight@EndHighlight}{
  \coordinate (end highlight) at (0,0) ;
}

\newdimen\highlight@previous
\newdimen\highlight@current

\DeclareRobustCommand*\highlight[1][]{%
  \tikzset{this highlighter/.style={#1}}%
  \SOUL@setup
  %
  \def\SOUL@preamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@BeginHighlight
      \highlight@EndHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@postamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@EndHighlight
      \highlight@DoHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@everyhyphen{%
    \discretionary{%
      \SOUL@setkern\SOUL@hyphkern
      \SOUL@sethyphenchar
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everyexhyphen##1{%
    \SOUL@setkern\SOUL@hyphkern
    \hbox{##1}%
    \discretionary{%
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everysyllable{%
    \begin{tikzpicture}[overlay, remember picture]
      \path let \p0 = (begin highlight), \p1 = (0,0) in \pgfextra
        \global\highlight@previous=\y0
        \global\highlight@current =\y1
      \endpgfextra (0,0) ;
      \ifdim\highlight@current < \highlight@previous
        \highlight@DoHighlight
        \highlight@BeginHighlight
      \fi
    \end{tikzpicture}%
    \the\SOUL@syllable
    \tikz[overlay, remember picture] \highlight@EndHighlight ;%
  }%
  \SOUL@
}
\makeatother


\begin{document}

\chapter{}

What the highlighting looks like:
\begin{proposition}\end{proposition}

What I want it to look like: \\
\textbf{\highlight{Satz 1.1. (my title)}}

\end{document}

This produces a document like this:

enter image description here

In particular notice, that the "randomness" in the highlighting gets removed (it's just a clear box around "Satz"). Also I want the highlighting to span the entire title (Satz, numbering, name).

How can I approach this?

  • 1
    @Schrödinger'scat I included a minimal example and a picture to highlight my problem :) – user7802048 Dec 7 '19 at 19:37
4

This is an expansion problem. You need to expand the argument of \highlight first. Here is one way to do it. It defines a variant, \Highlight, which expands the argument first. (Of course there are other ways, e.g. using \expandafter.)

\documentclass{report}

\usepackage{amsthm}
\usepackage{xcolor}
\usepackage{mdframed}

\definecolor{superlight}{HTML}{F5F5F5}
\mdfdefinestyle{basic-gray-box}{backgroundcolor=superlight}

\newcommand\Highlight[1]{\edef\temp{\noexpand\highlight{#1}}%
\temp}

\setlength{\parindent}{0pt}

\newtheoremstyle{mystyle}
  {}
  {}
  {\itshape}
  {}
  {\bfseries}
  {.}
  { }
  {\Highlight{\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}}}%  

\theoremstyle{mystyle}
\newmdtheoremenv[style=basic-gray-box]{proposition}{Satz}[chapter]

% Highlight
\usepackage{soul}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathmorphing}

\makeatletter

\newcommand{\defhighlighter}[3][]{%
  \tikzset{every highlighter/.style={color=#2, fill opacity=#3, #1}}%
}

\defhighlighter{yellow}{.5}

\newcommand{\highlight@DoHighlight}{
  \fill [ decoration = {random steps, amplitude=2pt, segment length=15pt}
        , outer sep = -30pt, inner sep = 0pt, decorate
        , every highlighter, this highlighter ]
        ($(begin highlight)+(0,8pt)$) rectangle ($(end highlight)+(0,-3pt)$) ;
}

\newcommand{\highlight@BeginHighlight}{
  \coordinate (begin highlight) at (0,0) ;
}

\newcommand{\highlight@EndHighlight}{
  \coordinate (end highlight) at (0,0) ;
}

\newdimen\highlight@previous
\newdimen\highlight@current

\DeclareRobustCommand*\highlight[1][]{%
  \tikzset{this highlighter/.style={#1}}%
  \SOUL@setup
  %
  \def\SOUL@preamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@BeginHighlight
      \highlight@EndHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@postamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@EndHighlight
      \highlight@DoHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@everyhyphen{%
    \discretionary{%
      \SOUL@setkern\SOUL@hyphkern
      \SOUL@sethyphenchar
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everyexhyphen##1{%
    \SOUL@setkern\SOUL@hyphkern
    \hbox{##1}%
    \discretionary{%
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everysyllable{%
    \begin{tikzpicture}[overlay, remember picture]
      \path let \p0 = (begin highlight), \p1 = (0,0) in \pgfextra
        \global\highlight@previous=\y0
        \global\highlight@current =\y1
      \endpgfextra (0,0) ;
      \ifdim\highlight@current < \highlight@previous
        \highlight@DoHighlight
        \highlight@BeginHighlight
      \fi
    \end{tikzpicture}%
    \the\SOUL@syllable
    \tikz[overlay, remember picture] \highlight@EndHighlight ;%
  }%
  \SOUL@
}
\makeatother


\begin{document}

\chapter{}

What the highlighting looks like:
\begin{proposition}[my title]
\end{proposition}

What I want it to look like: \\
\textbf{\highlight{Satz 1.1. (my title)}}

\end{document}

enter image description here

To put the dot on top of the highlighted region, you could use

\documentclass{report}

\usepackage{amsthm}
\usepackage{xcolor}
\usepackage{mdframed}

\definecolor{superlight}{HTML}{F5F5F5}
\mdfdefinestyle{basic-gray-box}{backgroundcolor=superlight}

\newcommand\Highlight[1]{\edef\temp{\noexpand\highlight{#1}}%
\temp}

\setlength{\parindent}{0pt}

\newtheoremstyle{mystyle}
  {}
  {}
  {\itshape}
  {}
  {\bfseries}
  {\hspace*{-1ex}.}
  { }
  {\Highlight{\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}~}}%  

\theoremstyle{mystyle}
\newmdtheoremenv[style=basic-gray-box]{proposition}{Satz}[chapter]

% Highlight
\usepackage{soul}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathmorphing}

\makeatletter

\newcommand{\defhighlighter}[3][]{%
  \tikzset{every highlighter/.style={color=#2, fill opacity=#3, #1}}%
}

\defhighlighter{yellow}{.5}

\newcommand{\highlight@DoHighlight}{
  \fill [ decoration = {random steps, amplitude=2pt, segment length=15pt}
        , outer sep = -30pt, inner sep = 0pt, decorate
        , every highlighter, this highlighter ]
        ($(begin highlight)+(0,8pt)$) rectangle ($(end highlight)+(0,-3pt)$) ;
}

\newcommand{\highlight@BeginHighlight}{
  \coordinate (begin highlight) at (0,0) ;
}

\newcommand{\highlight@EndHighlight}{
  \coordinate (end highlight) at (0,0) ;
}

\newdimen\highlight@previous
\newdimen\highlight@current

\DeclareRobustCommand*\highlight[1][]{%
  \tikzset{this highlighter/.style={#1}}%
  \SOUL@setup
  %
  \def\SOUL@preamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@BeginHighlight
      \highlight@EndHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@postamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@EndHighlight
      \highlight@DoHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@everyhyphen{%
    \discretionary{%
      \SOUL@setkern\SOUL@hyphkern
      \SOUL@sethyphenchar
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everyexhyphen##1{%
    \SOUL@setkern\SOUL@hyphkern
    \hbox{##1}%
    \discretionary{%
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everysyllable{%
    \begin{tikzpicture}[overlay, remember picture]
      \path let \p0 = (begin highlight), \p1 = (0,0) in \pgfextra
        \global\highlight@previous=\y0
        \global\highlight@current =\y1
      \endpgfextra (0,0) ;
      \ifdim\highlight@current < \highlight@previous
        \highlight@DoHighlight
        \highlight@BeginHighlight
      \fi
    \end{tikzpicture}%
    \the\SOUL@syllable
    \tikz[overlay, remember picture] \highlight@EndHighlight ;%
  }%
  \SOUL@
}
\makeatother


\begin{document}

\chapter{}

What the highlighting looks like:
\begin{proposition}[my title] 
blub bla blub
\end{proposition}

What I want it to look like: \\
\textbf{\highlight{Satz 1.1. (my title)}}

\end{document}

enter image description here

  • Thank you! Now it is working. One thing I still notice though is that the dot is on top of the highlighting (while the title is behind the highlighting). Do you see any way to put the whole highlighting behind the text? – user7802048 Dec 7 '19 at 19:55
  • @user7802048 This is because you have the dot separately. If you wish to have the dot highlighted, too, you could use \newtheoremstyle{mystyle} {} {} {\itshape} {} {\bfseries} {} { } {\Highlight{\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}.}} instead, where I moved the dot to the last argument. – Schrödinger's cat Dec 7 '19 at 19:58
  • Ok, this will put the dot behind the semi-transparent highlighting as well. To put the highlighting in the background would require to manipulate the code for highlight right? – user7802048 Dec 7 '19 at 20:00
  • @user7802048 Yes. In this case, unless the title runs over more than one line, the modification wouldn't be a tough thing to do because the you can just use a code that does not require soul. It is possible to have all the highlighting on the background, but this is a much tougher cookie. – Schrödinger's cat Dec 7 '19 at 20:03
  • What could I do in the case of single-lined titles? – user7802048 Dec 7 '19 at 20:04

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.