为了改变垂直位置,\overline
我发现了以下宏:
\newcommand*\oline[1]{%
\vbox{%
\hrule height 0.5pt% % Line above with certain width
\kern0.4ex% % Distance between line and content
\hbox{%
\kern-0.1em% % Distance between content and left side of box, negative values for lines shorter than content
\ifmmode#1\else\ensuremath{#1}\fi% % The content, typeset in dependence of mode
\kern-0.1em% % Distance between content and left side of box, negative values for lines shorter than content
}% end of hbox
}% end of vbox
}
但它在上标/下标中效果不好。例如参见以下 MWE:
\documentclass[12pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\newcommand*\oline[1]{%
\vbox{%
\hrule height 0.5pt% % Line above with certain width
\kern0.4ex% % Distance between line and content
\hbox{%
\kern-0.1em% % Distance between content and left side of box, negative values for lines shorter than content
\ifmmode#1\else\ensuremath{#1}\fi% % The content, typeset in dependence of mode
\kern-0.1em% % Distance between content and left side of box, negative values for lines shorter than content
}% end of hbox
}% end of vbox
}
\begin{document}
\section{title}
\[\overline{h},\overline{h_t},\overline{h_{t_1}},\overline{h}_{t_1}\]
\[\oline{h}, \oline{h_t}, \oline{h_{t_1}}, \oline{h}_{t_1}\]
\end{document}
及其输出:
如何修复此错误输出\oline
?
答案1
不良影响不仅限于下标:
\documentclass[12pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\newcommand*\oline[1]{%
\vbox{%
\hrule height 0.5pt% % Line above with certain width
\kern0.4ex% % Distance between line and content
\hbox{%
\kern-0.1em% % Distance between content and left side of box, negative values for lines shorter than content
\ifmmode#1\else\ensuremath{#1}\fi% % The content, typeset in dependence of mode
\kern-0.1em% % Distance between content and left side of box, negative values for lines shorter than content
}% end of hbox
}% end of vbox
}
\begin{document}
$a\oline{h}b$
\end{document}
问题在于您没有取消所应用的负字距,从而欺骗上划线使其比其上方应有的字符更短。
\documentclass[12pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\newcommand*\oline[1]{%
\kern0.1em % Counteract the inner kern
\vbox{%
\hrule height 0.5pt % Line above with certain width
\kern0.4ex % Distance between line and content
\hbox{%
\kern-0.1em % Shorten the content on the left
$#1$% % The content, typeset math mode
\kern-0.1em % Shorten the content on the right
}% end of hbox
}% end of vbox
\kern0.1em % Counteract the inner kern
}
\begin{document}
$a\oline{h}b+\oline{h}_{t}+\oline{h_t}$
$ahb+h_t+h_t$
\end{document}
\ifmmode
代码中的条件根本不执行任何操作,因为它出现在模式\hbox
为的内部,不是\vbox
math:TeX 在启动 a或 a时不会继承模式\hbox
:它分别以内部垂直模式和受限水平模式启动它们。因此,您只需启动数学模式即可。
紧跟%
在pt
、ex
和em
后面的字符是错误的:TeX 会在所需测量单位后寻找(并忽略)空格标记。省略空格可能会导致不合时宜的扩展问题。例如,\ifmmode
在实际放置 kern 之前,您的 就已扩展;在这种情况下,这并不是什么问题,因为条件被评估为 false。