初始问题:
我想用以下代码为文本添加连续下划线:
\uline{No underlining for \colorbox{lightgray}{everything} inside the colorbox.}
解决方法:
一个办法为了获得内部文本的连续行\colorbox
,请执行以下操作:
\sbox0{\uline{\hspace{\fboxsep}everything\hspace{\fboxsep}}}
\uline{No underlining for \colorbox{lightgray}{\hspace{-\fboxsep}\usebox0\hspace{-\fboxsep}} inside the colorbox.}
问题:
我该如何重新定义\uline
,以便每个\colorbox
内部\uline
都按照上面的解决方法所示进行本地定义,以便也具有下划线的文本?
麦格维:
\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage[normalem]{ulem}
\begin{document}
\uline{No underlining for \colorbox{lightgray}{everything} inside the colorbox.}
\end{document}
答案1
重新定义\uline
包括的重新定义\colorbox
。
\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage[normalem]{ulem}
\makeatletter
\long\def\afterelsefi#1\else#2\fi{\fi#1}
\long\def\afterfi#1\fi{\fi#1}
\def\q@mark{\q@mark}
\newcommand\uline@colorbox[3][\q@mark]
{%
\ifx\q@mark#1%
\afterelsefi\colorbox@orig{#2}%
\else
\afterfi\colorbox@orig[#1]{#2}%
\fi
{%
% nested \uline becomes a second rule which is a bit lower than first
% the following changes the values of \UL@height and \ULdepth so that the
% calculation done by ulem results in the original values
\advance\UL@height\ULdepth
\advance\ULdepth-\thr@@\UL@height
\hskip-\fboxsep
\uline{\hskip\fboxsep#3\hskip\fboxsep}%
\hskip-\fboxsep
}%
}
\let\colorbox@orig\colorbox
\protected\def\uline
{%
\relax
\ifmmode
\expandafter\underline
\else
\bgroup
\let\colorbox\uline@colorbox % this is added compared to the original definition
\expandafter\ULset
\fi
}
\makeatother
\begin{document}
\uline{No underlining for \colorbox{lightgray}{everything} inside the colorbox.}
\end{document}
答案2
这是使用节点的方法tikz
,在节点底部画一条线。但是,这不适用于文本换行,即如果文本比文本宽度宽,则会失败。
原则上,在我看来,下划线并不是最好的印刷标记方式。
\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{xcolor}
%\usepackage[normalem]{ulem}
\usepackage{tikz}
\newcommand{\underlinethis}[3][0.15\baselineskip]{%
\tikz[remember picture,baseline=(A.base)]{%
\node[inner sep=0pt,outer sep=0pt] (A) {#3}; % Place the node and typeset the text
\draw[#2] ([yshift=#1]A.south west) -- ([yshift=#1]A.south east); % Draw the line, shifted up by some value
}%
}
\begin{document}
\underlinethis{blue, line width=1pt}{No underlining for \colorbox{lightgray}{everything} inside the colorbox.}
\underlinethis{red, line width=1pt,dashed}{No underlining for \colorbox{lightgray}{everything} inside the colorbox.}
\end{document}
答案3
不要加下划线。曾经。
如果您不想遵循上述建议,请注意,当您调用\uline
宏\uline
时,它会再添加 1.2pt,因为它认为您想要双下划线。
\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage[normalem]{ulem}
\let\ulemuline\uline
\renewcommand{\uline}[1]{%
\begingroup
\redefinecolorbox
\ulemuline{#1}%
\endgroup
}
\let\latexcolorbox\colorbox
\newcommand\redefinecolorbox{%
\renewcommand\colorbox[2]{%
\latexcolorbox{##1}{%
\advance\ULdepth-1.2pt
\kern-\fboxsep
\ulemuline{\hspace{\fboxsep}##2\hspace{\fboxsep}}%
\kern-\fboxsep
}%
}%
}
\begin{document}
\uline{No underlining for \colorbox{lightgray}{everything} inside the colorbox.}
\end{document}