将文本置于非中心行的中心

将文本置于非中心行的中心

我需要一行包含三段文本的文本,分别向左对齐、向中间对齐和向右对齐,因此我尝试使用以下命令将它们分开\hfill

\documentclass{article}

\usepackage{lipsum}

\begin{document}
\noindent foo\hfill foo\hfill foo

\noindent foobar foobar foobar foobar foobar foobar\hfill foo\hfill foo

\noindent\lipsum[1]
\end{document}

如果文本长度相同,则可行,但如果文本长度不同,则不行。我理解,这\hfill会将行中的空白空间划分为与命令给出的次数相同的部分,并将它们放置在命令本身给出的位置。这意味着中心文本位于左侧和右侧之间的空白空间中,但我希望它位于左侧和右侧边距之间的中心。

为了更清楚起见,这是我得到的结果:

在此处输入图片描述

这就是我想要的:

在此处输入图片描述

我怎样才能做到这一点?

答案1

据我所知,你可以认为的推力效应\hfill与具有无限可膨胀性的压缩机械弹簧的推力效应相同。只要我内化了这个类比,我就不会遇到概念错误的情况。

下图试图以示意图的方式说明这一类比。

在此处输入图片描述

\documentclass[12pt]{article}

\usepackage{tikz}
\usepackage{circledsteps}
\usepackage{adjustbox}
\usepackage{xsavebox}

\usetikzlibrary{arrows.meta, decorations.pathmorphing, tikzmark, calc}

\setlength\parindent{0pt}

\tikzset{
    inline/.style = {
        baseline, xshift=-2pt
    },
    inline insert/.style = {
        overlay, inline
    },
    my mark/.style = { 
        green!40!black, thick
    },
    spring/.style = {
        {|[width=1em]}-{|[width=1em]},
        semithick,
        orange!95!black,
        /pgf/decoration/name=coil,
        /pgf/decoration/aspect=-0.37,
        /pgf/decoration/post length=0.4mm,
        /pgf/decoration/pre length=0.7mm,
        /pgf/decoration/amplitude=1.5mm,
        /pgf/decoration/segment length=#1
    },
    force/.style = {
        to path = {
            decorate {([yshift=1.5mm]\tikztostart) -- ([yshift=1.5mm]\tikztotarget)}
        },
        execute at begin to = {
            \begin{scope}[
                overlay,
                <-,
                help lines, 
                draw=red!70!black,
                shorten <= -2mm,
                >=latex
            ]
                \draw ([yshift=4.5mm]\tikztostart)  -- ++ (#1,0);
                \draw ([yshift=4.5mm]\tikztotarget) -- ++(-#1,0);
            \end{scope}
        }
    }
}


\newcommand\Mybox[1]{%
    \adjustbox{
        padding*=0.2em 1em 0.2em 1em,
        frame,
        minipage=\linewidth,
        margin*=0em 1em 0em 0em,
        precode={\sffamily}{}
    }{\sffamily\normalsize%
        #1
    }
}

\xsavebox{spring}{%
    \begin{tikzpicture}
        \draw[spring={0.9mm}, -, /pgf/decoration/post length=0.9mm, /pgf/decoration/pre length=1.5mm, 
        /pgf/decoration/aspect=-0.39,
        decorate] (0,0) -- (0.8,0);
    \end{tikzpicture}
}

\newcommand\Insert[1]{%
    \tikz[inline insert]{\draw[my mark, <-<, >=Latex] (0,0) -- (0,2.5em) node[rotate=50, anchor=south west, inner sep=0pt] {#1};}%
}

\newcommand\InlineSpring[1]{%
    \tikz{\draw[spring={#1}, force=0.3] (0,0) to (0.4,0)}%
}

\begin{document}
\small 
\Mybox{foobar foobar foobar foobar foobar foobar \Insert{\ttfamily \textbackslash hfill}foo \Insert{\ttfamily \textbackslash hfill}foo}\bigskip\bigskip

\Mybox{foobar foobar foobar foobar foobar foobar \Insert{\Circled{\xusebox{spring}}}foo \Insert{\Circled{\xusebox{spring}}}foo}

Imagine \texttt{\textbackslash hfill} directives as compressed springs being placed between boxes.\par\bigskip
\Mybox{foobar foobar foobar foobar foobar foobar\InlineSpring{0.35mm}foo\InlineSpring{0.35mm}foo}

The springs expand until the left and rightmost glyphs are pushed against the page margins.\par\bigskip
\Mybox{foobar foobar foobar foobar foobar foobar\tikzmark{1-start}\hfill\tikzmark{1-end}foo\tikzmark{2-start}\hfill\tikzmark{2-end}foo}

\begin{tikzpicture}[overlay, remember picture]
    \foreach \i in {1,2} {
        \draw[spring={1.9mm}, force=0.17] (pic cs:\i-start) to (pic cs:\i-end);
    }
\end{tikzpicture}

\end{document}

相关内容