You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MRTwuline.sty 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. \newcommand*\MRTwuline@version{0.0.3}
  2. \newcommand*\MRTwuline@date{2019-02-03}
  3. \NeedsTeXFormat{LaTeX2e}
  4. \RequirePackage{expl3,xparse,stackengine,scalerel,MRTif,MRTutil}
  5. \PassOptionsToPackage{normalem}{ulem}
  6. \RequirePackage{ulem}
  7. \ProvidesExplPackage
  8. {MRTwuline} {\MRTwuline@date}
  9. {\MRTwuline@version} {MS Word like underline using ulem or stackengine}
  10. \keys_define:nn { MRTwuline }
  11. {
  12. ,rule~ht .tl_set:N = \l__MRTwuline_rule_thickness_tl
  13. ,ruleht .meta:n = { rule~ht = { #1 } }
  14. ,math~dp .tl_set:N = \l__MRTwuline_math_depth_tl
  15. ,mathdp .meta:n = { math~dp = { #1 } }
  16. ,text~dp .tl_set:N = \l__MRTwuline_text_depth_tl
  17. ,textdp .meta:n = { text~dp = { #1 } }
  18. }
  19. \cs_generate_variant:Nn \keys_set:nn { nV }
  20. \tl_set:Nn \l__MRTwuline_rule_thickness_tl { 0.185ex }
  21. \tl_set:Nn \l__MRTwuline_math_depth_tl { 0.21ex }
  22. \tl_set:Nn \l__MRTwuline_text_depth_tl { 0.42ex }
  23. \bool_new:N \l__MRTwuline_luaul_bool
  24. \let\if@MRTwuline@tikzuline\iffalse
  25. \DeclareOption{tikzunderline}{\let\if@MRTwuline@tikzuline\iftrue}
  26. \DeclareOption{tUline}{\ExecuteOptions{tikzunderline}}
  27. \DeclareOption*{ \keys_set:nV { MRTwuline } \CurrentOption }
  28. \ProcessOptions\relax
  29. \NewDocumentCommand \MRTwulineSetup { +m }
  30. {
  31. \keys_set:nn { MRTwuline } { #1 }
  32. }
  33. % support lua-ul if available >>=
  34. \sys_if_engine_luatex:TF
  35. {
  36. \file_if_exist:nTF { lua-ul.sty }
  37. { \bool_set_true:N }
  38. { \bool_set_false:N }
  39. }
  40. { \bool_set_false:N }
  41. \l__MRTwuline_luaul_bool
  42. \bool_if:NTF \l__MRTwuline_luaul_bool
  43. {
  44. \dim_new:N \l__MRTwuline_luaul_dp_dim
  45. \dim_new:N \l__MRTwuline_luaul_wd_dim
  46. \dim_set:Nn \l__MRTwuline_luaul_dp_dim { \l__MRTwuline_text_depth_tl }
  47. \dim_set:Nn \l__MRTwuline_luaul_wd_dim { \l__MRTwuline_rule_thickness_tl }
  48. \keys_define:nn { MRTwuline }
  49. { luaul .bool_set:N = \l__MRTwuline_luaul_bool }
  50. \bool_set_true:N \l__MRTwuline_luaul_bool
  51. \PassOptionsToPackage{minimal}{lua-ul}
  52. \RequirePackage{lua-ul}
  53. \newunderlinetype \__MRTwuline_luaul_on:
  54. [
  55. \luaul@defaultcontext
  56. wd: \dim_use:N \l__MRTwuline_luaul_wd_dim
  57. dp: \dim_use:N \l__MRTwuline_luaul_dp_dim
  58. ]
  59. { \leaders \MRTwuline_text_rule:n \l__MRTwuline_luaul_dp_dim }
  60. }
  61. {
  62. \msg_new:nnn { MRTwuline } { key~needs~lua-ul }
  63. {
  64. The ~ used ~ key ~ `\l_keys_key_str' ~ is ~ only ~ available ~ if ~
  65. LuaTeX ~ is ~ used.
  66. }
  67. \keys_define:nn { MRTwuline }
  68. { luaul .code:n = { \msg_error:nn { MRTwuline } { key~needs~lua-ul } } }
  69. }
  70. \keys_define:nn { MRTwuline } { lua-ul .meta:n = { luaul = { #1 } } }
  71. % =<<
  72. % \WUline >>=
  73. \NewDocumentCommand \WUline { o m }
  74. {
  75. \mode_if_math:TF
  76. \MRTwuline_math:nn
  77. \MRTwuline_text:nn
  78. { #1 } { #2 }
  79. }
  80. \cs_new_protected:Npn \MRTwuline_math:nn #1
  81. {
  82. \IfNoValueTF { #1 }
  83. { \MRTwuline_math_out:nn \l__MRTwuline_math_depth_tl }
  84. { \MRTwuline_math_out:nn { #1 } }
  85. }
  86. \cs_new_protected:Npn \MRTwuline_math_out:nn #1 #2
  87. {
  88. \ThisStyle
  89. {
  90. \setbox0 \hbox { $\SavedStyle #2$ }
  91. \stackengine
  92. { #1 }
  93. { $ { \SavedStyle #2 } $ }
  94. { \rule { \wd0 } { \l__MRTwuline_rule_thickness_tl } }
  95. { U } { c } { F } { F } { S }
  96. }
  97. \underline { \vphantom { #2 } }
  98. }
  99. \cs_new_protected:Npn \MRTwuline_text:nn #1
  100. {
  101. \IfNoValueTF { #1 }
  102. { \MRTwuline_text_out:nn \l__MRTwuline_text_depth_tl }
  103. { \MRTwuline_text_out:nn { #1 } }
  104. }
  105. \bool_if:NTF \l__MRTwuline_luaul_bool
  106. {
  107. \cs_new_protected:Npn \MRTwuline_text_out:nn #1 #2
  108. {
  109. \bool_if:NTF \l__MRTwuline_luaul_bool
  110. {
  111. \group_begin:
  112. \dim_set:Nn \l__MRTwuline_luaul_dp_dim { #1 }
  113. \dim_set:Nn \l__MRTwuline_luaul_wd_dim
  114. { \l__MRTwuline_rule_thickness_tl }
  115. \__MRTwuline_luaul_on:
  116. #2
  117. \group_end:
  118. }
  119. {
  120. \bgroup
  121. \markoverwith { \MRTwuline_text_rule:n { #1 } }
  122. \ULon { #2 }
  123. }
  124. }
  125. }
  126. {
  127. \cs_new_protected:Npn \MRTwuline_text_out:nn #1
  128. {
  129. \bgroup
  130. \markoverwith { \MRTwuline_text_rule:n { #1 } }
  131. \ULon
  132. }
  133. }
  134. \cs_new_protected:Npn \MRTwuline_text_rule:n #1
  135. {
  136. \vrule
  137. \@height \dim_eval:n { \l__MRTwuline_rule_thickness_tl - #1 }
  138. \@depth \dim_eval:n { #1 }
  139. \scan_stop:
  140. }
  141. % =<<
  142. % \tUline >>=
  143. \if@MRTwuline@tikzuline
  144. \MRTutil@def\MRTwuline@tUline@width{\l__MRTwuline_rule_thickness_tl}
  145. \RequirePackage{tikz}
  146. \usetikzlibrary{calc}
  147. \RequirePackage{tikzpagenodes}
  148. \newlength\MRTwuline@tUline@length
  149. \newcount\MRTwuline@tUline@count
  150. \MRTutil@def\MRTwuline@gettikzxy#1#2#3% >>=
  151. % https://tex.stackexchange.com/a/58590/121799
  152. {%
  153. \tikz@scan@one@point\pgfutil@firstofone#1\relax
  154. \xdef#2{\the\pgf@x}%
  155. \xdef#3{\the\pgf@y}%
  156. }% =<<
  157. \tikzset{MRTwuline@tUline/.style={line~width=\MRTwuline@tUline@width}}
  158. \MRTutil@def\MRTwuline@EmptyOProcessor#1#2% >>=
  159. {%
  160. \MRTifEmptyTF{#2}%
  161. {\def\ProcessedArgument{#1}}%
  162. {\def\ProcessedArgument{#2}}%
  163. }% =<<
  164. \NewDocumentCommand{\tUline}% >>=
  165. {
  166. >{\MRTwuline@EmptyOProcessor{-0.35ex}}O{} % height
  167. >{\MRTwuline@EmptyOProcessor{0pt}}O{} % overhang
  168. >{\MRTwuline@EmptyOProcessor{\l__MRTwuline_rule_thickness_tl}}O{}
  169. +m
  170. }
  171. {%
  172. \def\MRTwuline@tUline@width{#3}%
  173. \tikz[remember~picture, overlay, baseline=(Begin.base)]
  174. {%
  175. \node[anchor=base,inner~sep=0pt,outer~sep=0pt,xshift=-#2]
  176. (Begin) {\strut};
  177. \MRTwuline@gettikzxy{($(Begin.base)-(current~page.south~west)$)}
  178. {\tUline@bx}{\tUline@by}%
  179. }%
  180. #4%
  181. \tikz[remember~picture,overlay, baseline=(End.base)]
  182. {%
  183. \node[anchor=base,inner~sep=0pt,outer~sep=0pt,xshift=+#2]
  184. (End) {\strut};%
  185. \MRTwuline@gettikzxy{($(End.base)-(current~page.south~west)$)}
  186. {\tUline@ex}{\tUline@ey}%
  187. }%
  188. \begin{tikzpicture}[overlay,remember~picture]
  189. \ifdim\dimexpr\tUline@ey-\tUline@by = 0pt
  190. % one line
  191. \draw[MRTwuline@tUline] ($(Begin.base)+(0,#1)$)
  192. -- ($(End.base)+(0,#1)$);%
  193. \else
  194. % multiple lines
  195. \draw
  196. ($(current~page~text~area.west)-(#2,0)$) node(WestLine){};
  197. \draw
  198. ($(current~page~text~area.east)+(#2,0)$) node(EastLine){};
  199. \tUline@drawBtoE{#1}
  200. \ifdim\dimexpr\tUline@by-\tUline@ey>\baselineskip
  201. % more than two lines
  202. \bgroup
  203. \tUline@drawMultipleLines{#1}{\baselineskip}
  204. \egroup
  205. \fi
  206. \fi
  207. \end{tikzpicture}%
  208. }% =<<
  209. \MRTutil@def\tUline@drawMultipleLines#1#2% >>=
  210. {%
  211. \MRTwuline@tUline@length=#2
  212. \MRTwuline@tUline@count=1
  213. \loop\ifdim
  214. \dimexpr
  215. \tUline@by-\MRTwuline@tUline@count\MRTwuline@tUline@length
  216. \relax
  217. >\tUline@ey
  218. \draw[MRTwuline@tUline]
  219. ($(Begin.base -| WestLine)+(0,#1)-
  220. (0,\MRTwuline@tUline@count\MRTwuline@tUline@length)$) --
  221. ($(Begin.base -| EastLine)+(0,#1)-
  222. (0,\MRTwuline@tUline@count\MRTwuline@tUline@length)$);
  223. \advance\MRTwuline@tUline@count by 1
  224. \repeat
  225. }% =<<
  226. \MRTutil@def\tUline@drawBtoE#1% >>=
  227. {%
  228. \draw[MRTwuline@tUline]
  229. ($(Begin.base)+(0,#1)$) -- ($(Begin.base -| EastLine)+(0,#1)$)
  230. ($(End.base)+(0,#1)$) -- ($(End.base -| WestLine)+(0,#1)$);%
  231. }% =<<
  232. \fi
  233. % =<<
  234. % vim: ft=tex fdm=marker fmr=>>=,=<< sw=2 ts=2 tw=80