在单个段落内,挂一个换行符,但不要挂新行

在单个段落内,挂一个换行符,但不要挂新行

我正在写一个列表,并希望挂线像这样工作

想要这条线挂

我不想为列表中的每个项目使用单独的段落,也不想使用列表环境;我将使用列表的整个文本作为新命令的输入参数,我希望除了将列表放在我想要的位置之外,还能够使其正确悬挂 - 因此我想要的列表类型看起来像这样(我在这里使用 minipage 环境,以便行会像图像中一样断开):

\documentclass{article}
\usepackage{units}
\begin{document}

\begin{minipage}{4.5cm}
\setlength{\parindent}{0cm}
\nicefrac{1}{2} lb.\ (about 1\nicefrac{1}{2} cups) thinly sliced yellow onions\\
2 (about 1 cup) sliced green bell peppers\\
2 to 3 Tb olive oil, if necessary\\
2 cloves mashed garlic\\
Salt and pepper to taste
\end{minipage}

\end{document}

但是,当然,这会产生如下输出:

尝试不挂起等

我知道这\hangindent可以解决段落问题,但我不想为每个项目使用单独的段落(即使我这样做了,也是\hangindent局部的,所以它必须放在每个项目的开头——也不好)。无论如何,当我尝试这样做时,希望它能以某种方式知道我想要什么(令人惊讶的是,它经常与许多其他 LaTeX 东西一起工作……),我得到了这个:

\documentclass{article}
\usepackage{units}
\begin{document}

\begin{minipage}{4.5cm}
\setlength{\parindent}{0cm}
\hangindent=1em
\nicefrac{1}{2} lb.\ (about 1\nicefrac{1}{2} cups) thinly sliced yellow onions\\
2 (about 1 cup) sliced green bell peppers\\
2 to 3 Tb olive oil, if necessary\\
2 cloves mashed garlic\\
Salt and pepper to taste
\end{minipage}

\end{document}

产生这个:

使用 hangindent 得到我不想要的东西

所以这并不奇怪,但这意味着我正式没有主意了。有人知道是否可以在单个段落中挂起换行,但不挂起新行吗?

答案1

对我来说不使用段落是没有意义的:

\documentclass{article}
\usepackage{units}
\begin{document}

\begin{minipage}{4.5cm}
\everypar{\setlength\hangindent{1em}}
\nicefrac{1}{2} lb.\ (about 1\nicefrac{1}{2} cups) thinly sliced yellow onions

2 (about 1 cup) sliced green bell peppers
2 to 3 Tb olive oil, if necessary

2 cloves mashed garlic

Salt and pepper to taste
\end{minipage}

\end{document}

答案2

由于使用\hangindent是显而易见的解决方案,因此它可能也是最好的解决方案。尽管如此,如果你想让你的文本就像它是一个段落一样,你总是可以选择弄乱语法:

\documentclass{article}
\begin{document}
 \newenvironment{hanginglines}[1][1em]
  {%
   \renewcommand\\{\par\hangindent#1}%
   \parindent0pt %
   \hangindent#1 %
  }
  {\par}

\begin{hanginglines}
 This is a very long line that I hope will wrap many times.  This is a very long line that I hope will wrap many times.
 \\
 This is a very long line that I hope will wrap many times.  This is a very long line that I hope will wrap many times.
 \\
 This is a very long line that I hope will wrap many times.  This is a very long line that I hope will wrap many times.
\end{hanginglines}
\end{document}

答案3

这是 Herbert 答案的附录,其中显示了如何将文本重新用作命令的参数,@user16001 正在寻找的内容:

\documentclass{article}
\usepackage{units}
\newenvironment{hangingpars}[1][1.2em]{%
  \parindent=0pt
  \everypar{\hangafter=1\hangindent=#1}}{\par}

\begin{document}
\newcommand{\SavedStuff}{
\begin{hangingpars}[1em]
  \nicefrac{1}{2}\,lb.\ (about 1\nicefrac{1}{2} cups) thinly sliced yellow
  onions

  2 (about 1 cup) green bell peppers

  2 to 3 Tb olive oil, if necessary

  2 cloves mashed garlic

  Salt an pepper to taste
\end{hangingpars}
}

\SavedStuff\bigskip

\noindent\begin{minipage}{4.5cm}
  \SavedStuff
\end{minipage}

\end{document}

答案4

O 仅将其作为替代方案提供,并且我非常清楚您最初的请求是为命令添加参数。话虽如此,在与这些问题斗争了一段时间之后,我完全同意您关于最小化标记的观点——我相信明智地使用环境最终会简化和澄清处理食谱的过程。请注意成分中没有格式(没有\\),并且可以自由调整方法的格式以适应。

\documentclass{article}
\usepackage{units}
\usepackage{etoolbox}

%% Thanks to egreg, a new lrbox that is *global*:
%% https://tex.stackexchange.com/questions/43046/beamer-content-in-box-register-does-not-survive-endframe/43063#43063
%% Necessary because the minipage in 'ingreds' does not survive outside the environment.
\cslet{lrbox*}\lrbox
\expandafter\patchcmd\csname lrbox*\endcsname{\setbox}{\global\setbox}{}{}
\cslet{endlrbox*}\endlrbox

\newsavebox{\myingreds}
\newsavebox{\mymethod}

\newenvironment{ingreds}{%
    \begin{lrbox*}{\myingreds}%
        \begin{minipage}[t]{4.5cm}% Change to suit
        \everypar{\hangindent1em}%
        \obeylines
}{%
        \end{minipage}%
    \end{lrbox*}%
}

\newenvironment{method}{%
    \begin{lrbox}{\mymethod}%
        \begin{minipage}[t]{6cm}% Change to suit
}{%
        \end{minipage}
    \end{lrbox}
    \usebox{\myingreds}\quad\usebox{\mymethod}
}

\begin{document}

\begin{ingreds}
    \nicefrac{1}{2} lb.\ (about 1\nicefrac{1}{2} cups) thinly sliced yellow onion
     2 (about 1 cup) sliced green bell peppers
     2 to 3 Tb olive oil, if necessary
     2 cloves mashed garlic
     Salt and pepper to taste
\end{ingreds}
\begin{method}
 In the same skillet, cook the onions and peppers slowly in olive oil for about 10 minutes, or until tender but not browned. Stir in the garlic and season to taste.
\end{method}

\end{document}

菜谱样本

相关内容