笔记:我正在寻找 LaTeX 解决方案。提供了 ConText 代码,这是一个很好的答案,但不符合我的目的。
我想在文本中给一些单词加下划线以显示句子的部分。正如评论中提到的,只有单词才应该加下划线,而单词之间的空格则不应该加下划线。最好保留连字符。不幸的是,包dash-dotted
中没有提供样式ulem
。
以下是使用 TikZ 的解决方案(取自用点划线划线)
\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\newcommand{\mydash}[1]{%
\tikz[baseline=(todotted.base)]{
\node[inner sep=1pt,outer sep=0pt] (todotted) {\strut #1};
\draw[dash dot] ([yshift=3pt]todotted.south west) -- ([yshift=3pt]todotted.south east);
\draw[dashed] ([yshift=2pt]todotted.south west) -- ([yshift=2pt]todotted.south east);
}%
}%
\begin{document}
\mydash{doguble line}
\lipsum[2]
\mydash{double line double line double line double line double line double line double line }
\lipsum[2]
\end{document}
不允许换行,这很糟糕。我想要一个不会影响其他文本的解决方案(不会加宽行之间的垂直空间,即点划线应该有零高度),并允许换行。
这里提高或降低 \hrulefill讨论了一些创建线条的方法。egreg 提供了以下 MWE:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\xhrulefill}{O{}}
{
\group_begin:
\severin_xhrulefill:n { #1 }
\group_end:
}
\keys_define:nn { severin/xhrulefill }
{
height .dim_set:N = \l_severin_xhrule_height_dim,
thickness .dim_set:N = \l_severin_xhrule_thickness_dim,
fill .skip_set:N = \l_severin_xhrule_fill_skip,
height .initial:n = 0pt,
thickness .initial:n = 0.4pt,
fill .initial:n = 0pt plus 1fill,
}
\cs_new_protected:Nn \severin_xhrulefill:n
{
\keys_set:nn { severin/xhrulefill } { #1 }
\leavevmode
\leaders\hrule
height \dim_eval:n { \l_severin_xhrule_thickness_dim + \l_severin_xhrule_height_dim }
depth \dim_eval:n { -\l_severin_xhrule_height_dim }
\skip_horizontal:N \l_severin_xhrule_fill_skip
\kern 0pt
}
\ExplSyntaxOff
\setlength{\textwidth}{6cm} % just for the test
\begin{document}
abc\hrulefill
abc\xhrulefill
abc\xhrulefill[height=-3pt]
abc\xhrulefill[height=-2pt,thickness=1pt,fill=3cm]
abc\xhrulefill[height=-3pt,thickness=1pt]
abx\xhrulefill[height=0pt,thickness=1ex]
\end{document}
我想知道它是否可以根据我的需要进行xparse
修改expl3
。
答案1
这是另一个 TikZ 解决方案(实际上是 TikZ + 灵魂解决方案),基于 Gonzalo Medina 的变体(https://tex.stackexchange.com/a/52336/105447)Antal Spector-Zabusky的答案(https://tex.stackexchange.com/a/6029/105447) 到“LaTeX 中的酷炫文本突出显示”。
它本质上使用soul
和一些soul
内部结构来为 TikZ 绘图提供坐标(我的术语,请参阅 Antal 的回答以获得更详细和更有见地的解释),使用remember picture
,这将需要两次编译才能完成。线条是用 绘制的,overlay
因此排版根本不会受到干扰。换行符和连字符应该可以正常工作,但不能使用分页符。唉,单词之间的空格也会加下划线。
\documentclass{article}
\usepackage{soul}
\usepackage{tikz}
\usetikzlibrary{calc}
\newlength\LineWidth
\setlength\LineWidth{0.7pt}
\newlength\baseliney
\setlength\baseliney{-1.5pt}
\colorlet{HLcolor}{black}
\makeatletter
\newcommand\highlight@DoHighlight{%
\draw[dash dot, HLcolor,line width=\LineWidth,opacity=.5] ($(begin highlight)+(0,\baseliney)$) -- ($(end highlight)+(0,\baseliney)$) ;
}
\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*\dashdotuline[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}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. \dashdotuline{Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas.} Mauris ut leo. Cras viverra
metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus
eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Cur-
abitur auctor semper nulla. \dashdotuline{Donec varius orci} eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi \dashdotuline{dashing
auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et,
tellus. Donec aliquet, tortor sed accumsan bibendum,} erat ligula aliquet magna,
vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse
ut massa. Cras nec ante. Pellentesque a nulla. \dashdotuline{Cum sociis natoque penatibus et
magnis dis parturient montes, nascetur ridiculus mus.} Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris.
\end{document}
结果是:
线宽的明显差异是观察者看到的现象。
更新:使用 egreg 的解决方案https://tex.stackexchange.com/a/267558/105447,也可以隐藏单词之间的下划线。使用:
\ExplSyntaxOn
\NewDocumentCommand{\dashdotulwbyw}{m}
{
\seq_set_split:Nnn \l_tmpa_seq { ~ } { #1 }
\seq_map_inline:Nn \l_tmpa_seq { \dashdotuline{##1}~ } \unskip
}
\ExplSyntaxOff
这需要\usepackage{xparse}
并创建一个\dashdotulwbyw
代表破折号-点-下划线-逐字的符号。我还没有广泛尝试过这个,但是考虑到 egreg 对此工作原理的解释,我看不出它为什么不能与先前定义的 很好地配合使用\dashdotuline
。
答案2
\underdashes
并且\underdots
可在 ConTeXt 中开箱即用。\underdashdots
可以使用一些元乐趣。
\startuseMPgraphic{rules:under:dashdot}
draw
((0,RuleDepth) -- (RuleWidth,RuleDepth))
shifted (0,RuleFactor*RuleOffset)
dashed dashpattern(on RuleFactor/2 off RuleFactor/2 on RuleThickness off RuleFactor/2)
withpen pencircle scaled RuleThickness
withcolor RuleColor ;
setbounds currentpicture to unitsquare xysized(RuleWidth,RuleHeight) ;
\stopuseMPgraphic
\definebar[underdashdot] [undergraphic][mp=rules:under:dashdot]
\definebar[underdashdots][underdashdot][continue=yes]
\starttext
\underdashes{
\input knuth
}
\underdots{
\input knuth
}
\underdashdots{
\input knuth
}
\stoptext