I try to construct a yellow rectangle (with tikz
) so that I can use it to
"highlight" a piece of text. Below you see a minimal example. It already looks
good, but how can I avoid the word "dummy" to be raised? All words should be located at
the same height.
\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[american,ngerman]{babel}
\usepackage{tikz}
\newcommand*{\yellowemph}[1]{%
\tikz\node[rectangle, fill=yellow, rounded corners, inner sep=0.3mm]{#1};%
}
\begin{document}
Just some \yellowemph{dummy} text to see what happens.
\end{document}
UPDATE
Thank you all for your help. Below, I included a short overview/comparison about the suggested approaches. Unfortunately, Versions 3 and 4 overwrite the text. Version 2 (soul) did not work within math mode (not even by wrapping it in \text{}
). I like Version 1. See this page for how to automatically determine if in math mode or not: \newcommand: How to determine if in math mode (and then include $ $)?
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[american,ngerman]{babel}
\usepackage{tikz}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% version 1: needs $ $ in math mode; not work across line boundaries
\newcommand*{\yellowemph}[1]{%
\tikz[baseline=(text.base)]\node(text)[rectangle, fill=yellow, rounded
corners, inner sep=0.3mm]{#1};%
}
% version 2: does not seem to work in math mode, not even with $ $
\usepackage{soul}% use \hl{} to highlight
% version 3: does not work across line boundaries
% see https://tex.stackexchange.com/questions/35319/a-boxed-alternative-with-minimal-spacing/35357#35357
\usetikzlibrary{calc, shapes}
\makeatletter
\newcommand*{\@DrawBoxHeightSep}{0.030em}%
\newcommand*{\@DrawBoxDepthSep}{0.025em}%
\newcommand{\@DrawBox}[3][red]{% #1=style, #2=height, #3=depth
\tikz[overlay,remember picture]{
\draw[#1]
($(bl)+(-0.2em,#2+\@DrawBoxHeightSep)$) rectangle
($(br)+(0.2em,-#3-+\@DrawBoxDepthSep)$);}
}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newdimen\@myBoxHeight%
\newdimen\@myBoxDepth%
\newcommand{\MyBox}[2][red]{%
\settoheight{\@myBoxHeight}{#2}% record height of box
\settodepth{\@myBoxDepth}{#2}% record height of box
\tikzmark{bl}#2\tikzmark{br}\@DrawBox[#1]{\@myBoxHeight}{\@myBoxDepth}% draw the box
}
\makeatother
% version 4: text gets overwritten
% see
% https://tex.stackexchange.com/questions/5959/cool-text-highlighting-in-latex
% \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=1pt, segment length=15pt}
, outer sep = -15pt, 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}
\noindent
word $\frac{1}{2}$ $\displaystyle\frac{1}{2}$\\
\yellowemph{word} \yellowemph{$\frac{1}{2}$}
\yellowemph{$\displaystyle\frac{1}{2}$} $\yellowemph{$\displaystyle\frac{1}{2}$}$; Version 1: does not work across lines\\
\hl{word} \hl{$\frac{1}{2}$} \hl{$\displaystyle\frac{1}{2}$}
%$\text{\hl{$\displaystyle\frac{1}{2}$}}$
; Version 2: soul, does not work \emph{within} math mode!\\
\MyBox[fill=yellow, draw=yellow, opacity=0.3]{word} \MyBox[fill=yellow,
draw=yellow, opacity=0.3]{$\frac{1}{2}$} \MyBox[fill=yellow, draw=yellow,
opacity=0.3]{$\displaystyle\frac{1}{2}$}
%$\MyBox[fill=yellow, draw=yellow, opacity=0.3]{$\displaystyle\frac{1}{2}$}$
; Version 3: overwrites text; does not work \emph{within} math mode or across lines\\
\highlight{word} \highlight{$\frac{1}{2}$}
\highlight{$\displaystyle\frac{1}{2}$} $\highlight{$\displaystyle\frac{1}{2}$}$;
Version 4: overwrites text
\end{document}
\tikz[baseline=(text.base)]\node (text) [rectangle, fill=yellow, rounded corners, inner sep=0.3mm]{#1};
for your case.