将文本置于水平线之间,并延伸至两侧边距

将文本置于水平线之间,并延伸至两侧边距

我想知道如何制作这样的事情:

----------------------------------   Text   -------------------------------------

用文字来说:文本居中。行与文本之间留有一定空间。行与文本中心垂直对齐,并延伸至两侧的边距,因此总长度为\textwidth。思路如下:

\documentclass{article}
\begin{document}
\centering{\rule[0.5ex]{4.5cm}{2pt} \hspace{0.6cm} Text \hspace{0.6cm} \rule[0.5ex]{4.5cm}{2pt}}
\end{document}

编辑:应该改变的是线的长度,而不是空间的长度。

答案1

可扩展规则可定制高度:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\makeatletter
  \newcommand*\variableheghtrulefill[1][.4\p@]{%
    \leavevmode
    \leaders \hrule \@height #1\relax \hfill
    \null
  }
\makeatother

\begin{document}

\lipsum[1]

\noindent
\variableheghtrulefill[.5ex]\quad Text\quad\variableheghtrulefill[.5ex]

\lipsum[2]

\noindent
\variableheghtrulefill[.5ex]\quad Wider text\quad\variableheghtrulefill[.5ex]

\lipsum[3]

\noindent
\variableheghtrulefill\quad
    No argument gives default height\quad\variableheghtrulefill

\lipsum[4]

\end{document}

编辑: 这是产生的输出:

上述代码的输出

编辑2: 抱歉,我完全忽略了有关规则垂直对齐的要求;作为一种过度补偿,以下代码试图彻底解决这个问题:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\makeatletter
  % User command: \genhrulefill
  % This time we make it full-fledged! (:-)
  %
  % SYNOPSIS
  %   - \genhrulefill[<base_height>][<thickness>]
  %       A rule of thickness <thickness> whose lower edge runs
  %       at a height of <base_height> above the baseline of the text.
  %   - \genhrulefill[<base_height>]
  %       A rule of default thickness (0.4pt) whose lower edge runs
  %       at a height of <base_height> above the baseline of the text.
  %   - \genhrulefill
  %       A rule of default thickness (0.4pt) whose lower edge runs
  %       along the baseline of the text.
  %   - \genhrulefill*[<thickness>]
  %       A rule of thickness <thickness> whose axis is aligned along
  %       the math axis.
  %   - \genhrulefill*
  %       A rule of default thickness (0.4pt) whose axis is aligned along
  %       the math axis.
  \newcommand*\genhrulefill{%
    \begingroup % paranoid -- it's because we use \@tempdima, etc.
    \@ifstar \@s@genhrulefill \@n@genhrulefill
  }

  \newcommand*\@n@genhrulefill[1][\z@]{%
    \setlength\@tempdimb{#1}%
    \@x@genhrulefill
  }
  \newcommand*\@x@genhrulefill[1][.4\p@]{%
    \@tempdima \@tempdimb
    \addtolength\@tempdima{#1}%
    \@genhrulefill
  }

  \newcommand*\@s@genhrulefill[1][.4\p@]{%
    \setbox\z@ \hbox{$\global \dimen@i \fontdimen22\textfont\tw@$}%
    \setlength\@tempdima{#1}%
    \@tempdimb \dimexpr \dimen@i-\@tempdima/\tw@
    \advance \@tempdima \@tempdimb
    \@genhrulefill
  }

  \newcommand*\@genhrulefill{%
    \leavevmode
    \leaders \hrule \@height \@tempdima \@depth -\@tempdimb \hfill
    \null
    \endgroup
  }
\makeatother

\begin{document}

\lipsum[2]

\noindent
\genhrulefill[.5ex][4pt]\quad
    All arguments are given\quad\genhrulefill[.5ex][4pt]

\lipsum[2]

\noindent
\genhrulefill[.5ex]\quad Default thickness\quad\genhrulefill[.5ex]

\lipsum[2]

\noindent
\genhrulefill\quad No arguments\quad\genhrulefill

\lipsum[2]

\noindent
\genhrulefill*[2pt]\quad $*$-form, explicit thickness\quad\genhrulefill*[2pt]

\lipsum[2]

\noindent
\genhrulefill*\quad $*$-form, default thickness\quad\genhrulefill*

\lipsum[2]

\end{document}

我认为命令的星号形式\genhrulefill应该是首选形式。

输出: 在此处输入图片描述

答案2

它具有可调节的厚度,并且垂直对齐规则,仅考虑文本中的上升部(而不是下降部,因为就阅读而言,它们在视觉上无关紧要)。

\documentclass{article}
\usepackage{lipsum}

\newcommand{\textbetweenrules}[2][.4pt]{%
  \par\vspace{\topsep}
  \noindent\makebox[\textwidth]{%
    \sbox0{#2}%
    \dimen0=.5\dimexpr\ht0+#1\relax
    \dimen2=-.5\dimexpr\ht0-#1\relax
    \leaders\hrule height \dimen0 depth \dimen2\hfill
    \quad #2\quad
    \leaders\hrule height \dimen0 depth \dimen2\hfill
  }\par\nopagebreak\vspace{\topsep}
}

\begin{document}

\lipsum[2]

\textbetweenrules{Text}

\lipsum[3]

\textbetweenrules[3pt]{\Large aapaa}

\lipsum[3]

\end{document}

调整间距。这样就不会允许在有规则的文本后出现分页符。

在此处输入图片描述

答案3

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\noindent\lipsum[1]

\noindent\rule[0.5ex]{4.5cm}{2pt}\hfill{}Text\hfill \rule[0.5ex]{4.5cm}{2pt}

% fixed space around, but the width and depth of rule is not changeable
\noindent\hrulefill\hspace{.6cm}Text\hspace{.6cm}\hrulefill
\end{document}

在此处输入图片描述

相关内容