当我试图回答时,我如何自定义 fbox 和 hrule?,我尝试使用基本的 LaTeX 命令来执行此操作,如下面的 MWE 所示。但由于某种原因,该命令\egline
没有生成与相同的输出\sampleline
。这是为什么?
\documentclass[letterpaper]{article}
\usepackage{xcolor}
\usepackage{lipsum}
\newcommand{\egline}{%
\par\vskip 1ex%
{\color{ejemplo}%
\leavevmode\hrule height 1pt width \dimexpr\hsize-2em\relax \hrule height 0.5ex width 2em%
}%
\par\vskip1ex%
}
\definecolor{ejemplo}{RGB}{37,153,116}
\newcommand{\sampleline}{%
\par\noindent%
{\color{ejemplo}%
\parbox{\linewidth}{%
\hrulefill\rule{2em}{0.5ex}}%
}%
\par%
}
\begin{document}
\lipsum[1-2]
\egline
\sampleline
\lipsum[1-2]
\end{document}
我重新定义命令\egline
如下:
\newcommand{\egline}{%
\par\vskip 1ex%
{%
\color{ejemplo}%
\leavevmode%
\vbox{%
\hrule height 1pt width \dimexpr\hsize-2em\relax}%
\vbox{%
\hrule height 0.5ex width 2em}%
}%
\par\vskip1ex%
}
但它给出了额外的空白(水平方向 15pt)?奇怪吗?参见下图:
按照戴维·卡莱尔 (David Carlisle) 的建议进行第三次定义,我对上述问题进行了简单的修复,但我仍然不明白是什么导致了缩进。
\newcommand{\egline}{%
\par\vskip 1ex%
{%
\color{ejemplo}%
\hskip-15pt\leavevmode\vrule height 1pt width \dimexpr\hsize-2em\relax\vrule height 0.5ex width 2em%
}%
\par\vskip1ex%
}
总而言之,修复方法\noindent
是\leavevmode
:
\noindent\vrule height 1pt width \dimexpr\hsize-2em\relax\vrule height 0.5ex width 2em%
答案1
\newcommand{\egline}{%
\par\vskip 1ex%
% here you are in vertical mode
{%
%here you put a colour whatsit on the vertical list (best to avoid that if possible)
\color{ejemplo}%
% here you start a paragraph and get into horizontal mode
\leavevmode
% \hrule isn't allowed in hmode, so this ends the (blank) paragraph and gets back to vmode
\hrule height 1pt width \dimexpr\hsize-2em\relax
% as we are in vmode this rule comes under the previous one
\hrule height 0.5ex width 2em%
}%
% another color whatsit gets inserted here (again on a vertical list)
\par\vskip1ex%
}