Vertical Centered Horizontal Line on the same line as text?

1,174

To also specify the depth you can add \@depth<some length> right after \@height#1 (or right before, the order does not matter). A version of \vhrulefill that also lets you specify the depth as #2 would thus be:

\makeatletter %% <- change @ so that it can be used in command sequences
\def\myrulefill#1#2{\leavevmode\leaders\hrule\@height#1\@depth#2\hfill \kern\z@}
\makeatother  %% <- change @ back

Once you have defined this, you can produce a line whose top is raised <top from the baseline and whose bottom is raised <bottom> with \myrulefill{<top>}{-<bottom>}. Note the minus sign, which is necessary because the depth of a rule is actually the distance it extends below the baseline.

You do not specify what you mean by "centred", but I can think of at least three possibilities:

  1. Centred w.r.t. the capital letter X

    This seems to be what your picture indicates. You can produce such a line of thickness #1 with:

    \newcommand*\crulefill[1]{\myrulefill
        {\dimexpr(\fontcharht\font`X+#1)/2}{\dimexpr(-\fontcharht\font`X+#1)/2}}
    

    I'm using \fontcharht to obtain the height of the letter X (like here) in the current font and \dimexpr to do computations with dimensions (like here).

  2. Centred w.r.t. the lowercase letter x

    The letter x has a height of 1ex, so if this is what you want you can centre your rule at .5ex using:

    \renewcommand*\crulefill[1]{\myrulefill{\dimexpr(1ex+#1)/2}{\dimexpr(-1ex+#1)/2}}
    
  3. Centred w.r.t. the math axis

    The math axis is the imaginary line on which mathematical operators like +, -, * and for instance \rightarrow and the bar in fractions are centred. For Computer Modern, the math axis is positioned slightly higher than .5ex above the baseline. Here's how you can get a rule centred on the math axis:

    \makeatletter
    \renewcommand*\crulefill[1]{%
      \settoheight\@tempdima{$\m@th\vcenter{}$}%
      \myrulefill{\dimexpr\@tempdima+#1/2}{\dimexpr-\@tempdima+#1/2}%
    }
    \makeatother
    

    This uses \vcenter{} to create an empty box centred on the math axis and stores its height in the scratch register \@tempdima using \settoheight.f

(The % at the end of some lines are there for reasons explained in this answer by the way.)

Demonstration:

\documentclass{article}

\makeatletter %% <- change @ so that it can be used in command sequences
\def\myrulefill#1#2{\leavevmode\leaders\hrule\@height#1\@depth#2\hfill \kern\z@}
\makeatother %% <- change @ back

\begin{document}

% ================================== %
%  Centred at half the height of X:  %
% ================================== %
\newcommand*\crulefill[1]{\myrulefill
  {\dimexpr(\fontcharht\font`X+#1)/2}{\dimexpr(-\fontcharht\font`X+#1)/2}}

X \crulefill{1pt} $\leftarrow$ centred w.r.t.\@ X.

% ================================== %
%  Centred at half the height of x:  %
% ================================== %
\renewcommand*\crulefill[1]{\myrulefill{\dimexpr(1ex+#1)/2}{\dimexpr(-1ex+#1)/2}}

x \crulefill{1pt} $\leftarrow$ centred w.r.t.\@ x.

% =========================== %
%  Centred on the math axis:  %
%  =========================== %

\makeatletter
\renewcommand*\crulefill[1]{%
  \settoheight\@tempdima{$\m@th\vcenter{}$}%
  \myrulefill{\dimexpr\@tempdima+#1/2}{\dimexpr-\@tempdima+#1/2}%
}
\makeatother

$+$ \crulefill{1pt} $\leftarrow$ centred w.r.t.\@ math axis.

\end{document}

enter image description here

Share:
1,174

Related videos on Youtube

noobycoder
Author by

noobycoder

Updated on September 18, 2020

Comments

  • noobycoder
    noobycoder about 3 years

    I'm trying to modify awesome cv so that the horizontal line is centered vertically in the line. trying to center horizontal line vertically

    The relevant line of code in awesome-cv.cls is the following declaration:

    \def\vhrulefill#1{\leavevmode\leaders\hrule\@height#1\hfill \kern\z@}

    I've read another answer that says to use depth to do it, but after playing with it for a while I just can't seem to get it right. Please help.