我正在尝试找到一个可以在段落内使用的 Latex 命令,该命令用于创建空白,直到达到距左边距的某个指定距离,然后文本恢复。如果已达到指定距离,它应该不执行任何操作。
它应该保持左对齐,并且任何后续行都应从正常边距开始 - 这些是我无法管理的部分。
例如,如果在每个单词“part”之后都创建了这个水平空间:
A first part Lorem ipsum dolor sit amet, consectetur adipisc-
ing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliq-
ua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...
A longer first part Lorem ipsum dolor sit amet, consectetur adipisc-
ing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliq-
ua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...
A much much much much much longer first part Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation...
Part Lorem ipsum dolor sit amet, consectetur adipisc-
ing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliq-
ua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...
答案1
看起来您想要一个占用最小空间的段落标题。
您需要获取段落标题的文本,将其与所需长度进行比较,如果标题较短,则跳过剩余空间。因此您需要一个条件。使用“etoolbox”,类似以下内容可能会满足您的要求:
\documentclass[]{article}
\usepackage{etoolbox}
\usepackage{lipsum}
\newlength{\myheadlength}
\newlength{\requestedheadspace}
\newlength{\addedheadspace}
\setlength{\requestedheadspace}{.4\textwidth}% <--adjust to taste
\newcommand{\parstart}[1]{%
\settowidth{\myheadlength}{#1}%
\setlength{\addedheadspace}{\requestedheadspace}
% %compare to required width
\ifdimless{\myheadlength}{\requestedheadspace}{% then
\addtolength{\addedheadspace}{-\myheadlength}%
#1\hspace{\addedheadspace}%
}%
{%else just print the header
#1%
}%
}
\setlength{\parskip}{\baselineskip}
\setlength{\parindent}{0pt}
\raggedright
\begin{document}
\parstart{Dummy}\lipsum[1]
\parstart{Less short dummy}\lipsum[1]
\parstart{And a very very long dummy to go beyond the requested space} Some text \lipsum[1]
\end{document}
(使用 PdfLatex 和 LuaLatex 测试)
答案2
您也可以使用 itemize 样式环境来实现这一点。我不知道在您的情形下使用环境是否可取,但可能值得考虑。
下面我使用enumitem
包来创建parswithheads
环境(将被替换为更鼓舞人心的名称)并调整其缩进(参见文档了解更多信息)。
\documentclass{article}
%% To match your desired output:
%% (sets parindent to 0pt and parskip to .5\baselineskip plus 2pt)
\usepackage{parskip}
\usepackage{enumitem}
\newlist{parswithheads}{itemize}{1}
\setlist[parswithheads]{
align=left, %% alignment of labels
listparindent=\parindent, %% same indentation as in normal text
parsep=\parskip, %% same parskip as in normal text
topsep=0pt, %% vertical space above and below list
partopsep=0pt, %% extra vertical space above and below if separate paragraph
itemsep=0pt, %% no extra space between items
font=\normalfont\bfseries, %% font used for labels, you may want to remove \bfseries
leftmargin=0pt, %% don't indent the entire paragraph
itemindent=4cm, %% how much to indent the first line by (at least)
labelsep=.5em, %% space between label and text
labelindent=0pt, %% label starts at the left margin
labelwidth=! %% calculated from other values
}
\begin{document}
\framebox[4cm]{$\longleftarrow$\hfill 4cm\hfill$\longrightarrow$}
\begin{parswithheads}
\item[A first part]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris\ldots
\item[A longer first part]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris\ldots
\item[A much much much much much longer first part]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris\ldots
\item[Part]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris\ldots
\end{parswithheads}
\end{document}
以下是所涉及长度的概述。我将leftmargin
和设置labelindent
为0pt
,设置itemindent
为4cm
和labelsep
设置为.5em
(默认情况下正常空间可以拉伸到的最大量)。我添加了labelwidth=!
,以便从其他长度计算出为标签保留的空间量(因此4cm-.5em
在本例中也是如此)。如果实际标签比此长度宽,则会导致项目进一步缩进(如所需)。
无论如何,结果如下:
编辑:如果您认为enumitem
仅为此加载有点过度(我可以想象),那么下面的方法也应该有效。结果(据我所知)难以区分。
\documentclass{article}
%% To match your desired output:
%% (sets parindent to 0pt and parskip to .5\baselineskip plus 2pt)
\usepackage{parskip}
\newlength{\partitlewidth}
\setlength{\partitlewidth}{4cm}
\newcommand*\partitle[1]{%
\begingroup %% To limit the scope of box 0
\savebox0{\textbf{#1}\hspace{.5em}}% %% Save the title in a box, with some added space
\ifdim\wd0<\partitlewidth\wd0=\partitlewidth\fi %% Set width of box 0 to \partitlewidth if it is shorter
\usebox0% %% Print the box
\endgroup
\ignorespaces %% Ignore any ensuing following spaces
}
\begin{document}
\framebox[4cm]{$\longleftarrow$\hfill 4cm\hfill$\longrightarrow$}
\partitle{A first part}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris\ldots
\partitle{A longer first part}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris\ldots
\partitle{A much much much much much longer first part}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris\ldots
\partitle{Part}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris\ldots
\end{document}
宏\partitle
将其参数放在一个“框”(连同空格)中,并\partitlewidth
在打印框之前将此框的宽度设置为(如果宽度较短)。宏后面的任何空格都将被忽略。(这是必要的,因为空格可以拉伸/收缩,否则会改变缩进。)