如何在每行的开始和结束处添加文字?

如何在每行的开始和结束处添加文字?

我想在每行末尾添加一些文本。例如,对于输入

This is a long text that will be split by LaTeX into various lines.

我想获得

This is a long textX
Xthat will be splitX
Xby LaTeX into variousX
Xlines.

在每行的开始和结束处添加 X。

动机:我想要\hlsoul包中用填充包围文本,类似于\colorbox但能够将文本分成几行:

\newcommand\myHl[1]{{\Large\hl{\normalsize~#1~}}}

(请注意 周围的空格#1)。但是,当文本被分成几行时,突出显示的跨度不会在行首和行末显示此额外边距:

This is a long text
that will be split
by LaTeX into various
lines.

请注意最开始和最结束处的边距,但内行中没有边距。在每行的开头和结尾添加一个空格将添加所需的边距:

This is a long text
that will be split
by LaTeX into various
lines.

梅威瑟:

\documentclass{article}
\usepackage{color,soul}
\usepackage{array}

\newcommand\myHlWithoutLarge[1]{\hl{~#1~}}
\newcommand\myHlWithLarge[1]{{\Large\hl{\normalsize~#1~}}}

\begin{document}
    I want the text to be highlighted \myHlWithLarge{like this} and not \hl{like this}.

    \renewcommand\arraystretch{2}
    \begin{tabular}{l>{\raggedright\arraybackslash}p{3.8cm}|>{\raggedright\arraybackslash}p{3cm}}

    Normal \verb|\hl|        & This is \hl{short text} here
                             & no margin \\

                             & Now \hl{this is a long text that will be split by LaTeX into various lines} when typeset \\

    \verb|\myHlWithoutLarge| & This is \myHlWithoutLarge{short text} here
                             & left / right margin \\
                             
                             & Now \myHlWithoutLarge{this is a long text that will be split by LaTeX into various lines} when typeset \\

    \verb|\myHlWithLarge|    & This is \myHlWithLarge{short text} here
                             & margin around: what I want \\

                             & Now \myHlWithLarge{this is a long text that will be split by LaTeX into various lines} when typeset
                             & but ugly on the inner\- lines!\\
    \end{tabular}
\end{document}

结果

相关内容