如何在行中间制作文本:------- 文本示例 -------

如何在行中间制作文本:------- 文本示例 -------

我想在某些文本的两侧添加水平线。我尝试使用\rule,但我希望多行文本的总长度(文本 + 行)相同(且由用户定义),因此行长度取决于文本长度。示例:

Tree text-lines

是否有一个包或一个本机的 LaTeX 命令可以完成这个技巧?

答案1

\documentclass{article}
\usepackage{xhfill}
\begin{document}        
\noindent
\xrfill[0.7ex]{1pt}Text\xrfill[0.7ex]{1pt}

\noindent
\xrfill[0.7ex]{1pt}This is a longer text\xrfill[0.7ex]{1pt}
\end{document}

enter image description here

答案2

这里,我采用了 的定义\hrulefill,并为 添加了高度/宽度/深度规范\hrule,使其可以自定义(我称之为\myrulefill)。此外,通过避免之前建议的\raisebox,文本位于基线上。

\documentclass{article}
\def\dotfill#1{\cleaders\hbox to #1{.}\hfill}
\makeatletter
\def\myrulefill{\leavevmode\leaders\hrule height .7ex width 1ex depth -0.6ex\hfill\kern\z@}
\makeatother
\begin{document}
\noindent\myrulefill Short Text\myrulefill\par

\noindent\myrulefill This is Longer Text\myrulefill

\noindent\myrulefill What you see is very long text\myrulefill

\makeatletter
\def\myrulefill{\leavevmode\leaders\hrule height .9ex width 1ex depth -0.2ex\hfill\kern\z@}
\makeatother

\noindent\myrulefill Short Text\myrulefill\par

\noindent\myrulefill This is Longer Text\myrulefill

\noindent\myrulefill What you see is very long text\myrulefill

\end{document}

enter image description here

答案3

\documentclass[a4paper,11pt]{article}
\usepackage{array}
\usepackage{ragged2e}

%% Define a new command as Steven B. Segletes suggested:
%% Use the length \rlength to have a default width.
\newlength{\rlength}\setlength{\rlength}{5cm}
\newcommand{\ruletext}[2][\rlength]{%
  \noindent%
  \parbox{#1}{%
    \noindent\hrulefill\raisebox{-.3\ht\strutbox}{#2}\hrulefill\par}%
}

\begin{document}

\ruletext{Foo}

\ruletext{Bar Baz}

%% Change the width of the box.
\ruletext[3cm]{Hello World}
\end{document}

结果是

enter image description here

相关内容