TeX - LaTeX Asked by Tera on May 12, 2021
I would like to define a command for highlighting in the following style.
Some people do this on paper and I would like to mimic that on a particular document. How could I achieve this? I thought of using the package tcolorbox
, but I am at a loss as to how to approach it using the package and as it is simple perhaps it could be done without using this package. I would like the command to receive as arguments the colour of the highlighting and the content to be highlighted. I would like this to work with XeLaTeX
.
EDIT: I’ve tested @abcdefg’s solution and the following problem arose.
How can I make the rectangle be always at the same level? It would be nice if it could begin a little bellow the line base and advance up to the middle height of the characters. It would also be nice if the behaviour of the macro were favorable to line breaks.
Here I use ooalign
to get the rule, and a tokencycle to achieve line breaking (however, hyphenation is lost, which may require sloppypar
to remedy, in some cases, such as the MWE).
EDITED to use discretionary
in the Spacedirective
, to avoid hanging spaces when linebreaking.
REEDITED to add some glue to assist with justification.
documentclass[10pt]{article}
usepackage{xcolor,tokcycle}
newlengthreplength
newcommandruleht{-1.5pt}% ELEVATION OF RULE
newcommandrulewidth{4pt}% THICKNESS OF RULE
defrulecolor{purple!30}% COLOR OF RULE
newcommandsphl[1]{sbox0{#1}ooalign{makebox[0pt][l]{%
smash{color{rulecolor}rule[ruleht]{wd0}{rulewidth}}}cr#1}}
newcommandmyul[2]{%
resettokcycle
Characterdirective{addcytoks{nobreakhspace{0pt minus.6pt}%
sphl{##1}}}%
Spacedirective{addcytoks{nobreakhspace{0pt minus .6pt}%
discretionary{}{}{sphl{ }}}}%
cytoks{}%
defrulecolor{#1}%
tokcyclexpress{#2}%
thecytoks%
}
begin{document}
begin{sloppypar}
Now I will try my special underlining myul{purple!25}{%
This is a test, we will
see if word wrapping occurs automatically or not
This is a test, we will
see if word wrapping occurs automatically or not
This is a test, we will
see if word wrapping occurs automatically or not
This is a test, we will
see if word wrapping occurs automatically or not
This is a test, we will
see if word wrapping occurs automatically or not
This is a test, we will
see if word wrapping occurs automatically or not} back to normal text.
end{sloppypar}
noindenthrulefill
end{document}
Correct answer by Steven B. Segletes on May 12, 2021
tcolorbox
is arguably one of the most powerful option if you seek extensions later on.
documentclass{article}
usepackage{tcolorbox}
tcbuselibrary{skins}
usetikzlibrary{calc}
tcbset{twilight/.style={enhanced,hbox,colback=white,
underlay={begin{tcbclipinterior}
fill[#1] let p1=($(interior.north)-(interior.south)$) in
([yshift={0.5*y1}]interior.south west) rectangle
(interior.south east);
end{tcbclipinterior}}},twilight/.default=purple!50,
tight/.style={on
line,boxsep=0pt,left=#1,right=#1,top=#1,bottom=#1,arc=0pt,boxrule=0pt,colframe=white},
tight/.default=0pt}
begin{document}
One wishes to highlight
begin{tcolorbox}[tight,twilight]
this expression
end{tcolorbox} in a sentence.
end{document}
Answered by user228539 on May 12, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP