我有一段长度为一行或两行的段落。我希望两个连续段落 A 和 B 的开头之间的距离保持不变。我该如何实现?
基本上,如果 A 只有一行,我希望有一个“幽灵空白行”,因此 B 的开头与 A 的开头之间的距离等于 A 有两行时的距离(我不想手动为只有一行的段落添加垂直空格)
编辑:
这是一个 MWE(请注意,我使用表格而不是评论中所建议的段落)
我希望这两个 tikz 文本节点的行从相同的高度开始。我不太担心 的参数的混乱,\content
因为我将以编程方式生成它。(使用 tikz 文本节点是必要的。使用 tabularx 不是必需的,但格式应该与我下面的格式相匹配,即左侧的数字和段落的其余部分与左侧对齐(This 的 T 位于 will 的 w 之上))
如果我意外添加了三行或更长的段落,则会生成警告或错误,从而获得加分。
\documentclass[a4paper]{article}
\usepackage{tikz}
\renewcommand{\arraystretch}{1.7}
\usepackage{tabularx}
\newcommand{\content}[1]{
\node[text width=5cm] {
\scriptsize
\begin{tabularx}{\textwidth}{@{} r @{} X @{}}
#1
\end{tabularx}
};
}
\begin{document}
\begin{minipage}{0.5\textwidth}
\begin{tikzpicture}
\content{ \textbf{1. } & \textbf{This text contains two lines, I will never write three lines} \\ \textbf{2. } & \textbf{One line} \\ \textbf{3. } & \textbf{One line} \\ \textbf{4. } & \textbf{One line} \\ \textbf{5. } & \textbf{One line} \\ \textbf{6. } & \textbf{One line} \\ \textbf{7. } & \textbf{One line} \\ \textbf{8. } & \textbf{One line} \\ \textbf{9. } & \textbf{One line} \\ \textbf{10. } & \textbf{One line}}
\end{tikzpicture}
\end{minipage}%
\begin{minipage}{0.5\textwidth}
\begin{tikzpicture}
\content{ \textbf{1. } & \textbf{This text contains two lines, I will never write three lines} \\ \textbf{2. } & \textbf{This text contains two lines, I will never write three lines} \\ \textbf{3. } & \textbf{This text contains two lines, I will never write three lines} \\ \textbf{4. } & \textbf{This text contains two lines, I will never write three lines} \\ \textbf{5. } & \textbf{This text contains two lines, I will never write three lines} \\ \textbf{6. } & \textbf{This text contains two lines, I will never write three lines} \\ \textbf{7. } & \textbf{This text contains two lines, I will never write three lines} \\ \textbf{8. } & \textbf{This text contains two lines, I will never write three lines} \\ \textbf{9. } & \textbf{This text contains two lines, I will never write three lines} \\ \textbf{10. } & \textbf{This text contains two lines, I will never write three lines}}
\end{tikzpicture}
\end{minipage}
\end{document}
答案1
这是一个完全不同的想法:假设你处于 中tikzpicture
,使用\path
动作......
\documentclass[a4paper]{article}
\usepackage{tikz}
\overfullrule=5pt
\newcommand{\oneline}{One line of text}
\newcommand{\twolines}{This text contains two lines, I will never write three lines}
\newcommand{\slap}[2]{%
\path (start) node[anchor=base west, text width=0.5cm,
font=\scriptsize\bfseries, inner xsep=0.2cm, align=right](tmp){#1}
(tmp.base east) node[anchor=base west, text width=4.8cm, align=left,
font=\scriptsize](tmp){#2.};
\coordinate (start) at ([yshift=-0.7cm]start);
}
\begin{document}
\noindent\begin{minipage}[t]{0.5\textwidth}
\begin{tikzpicture}[baseline]
\coordinate (start) at (0,0);
\slap{1}{\oneline}
\slap{2}{\twolines}
\slap{3}{\twolines}
\slap{4}{\oneline}
\slap{5}{\oneline}
\slap{6}{\oneline}
\slap{7}{\oneline}
\slap{8}{\oneline}
\slap{9}{\oneline}
\slap{10}{\oneline}
\end{tikzpicture}
\end{minipage}%
\begin{minipage}[t]{0.5\textwidth}
\begin{tikzpicture}[baseline]
\coordinate (start) at (0,0);
\slap{1}{\twolines}
\slap{2}{\twolines}
\slap{3}{\twolines}
\slap{4}{\twolines}
\slap{5}{\twolines}
\slap{6}{\oneline}
\slap{7}{\oneline}
\slap{8}{\twolines{} And if I wrote too much, it'll overlap: \twolines}
\slap{9}{\oneline}
\slap{10}{\oneline}
\end{tikzpicture}
\end{minipage}%
\end{document}
对于额外的一点,您可以测量盒子是否大于运动,并在这种情况下发出错误:
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\newcommand{\myerror}{%
\@latex@error
{Too many lines here}% Error message
{The box is overlapping with the previous one}% Help text
}
\makeatother
\overfullrule=5pt
\newcommand{\oneline}{One line of text}
\newcommand{\twolines}{This text contains two lines, I will never write three lines}
\newcommand{\slap}[2]{%
\path (start) node[anchor=base west, text width=0.5cm, font=\scriptsize\bfseries, inner xsep=0.2cm, align=right](tmp){#1.}
(tmp.base east)
node[anchor=base west, text width=4.8cm, align=left, font=\scriptsize](tmp){#2.};
\coordinate (start) at ([yshift=-0.7cm]start);
% let's calculate the vertical distance between (tmp.south) and (start)
\path let \p1=(start),\p2=(tmp.south),\n1={\y1-\y2} in \pgfextra{\xdef\tmpp{\n1}};
% you may need to tweak the "0pt" here
\ifdim\tmpp>0pt\myerror\fi
}
\begin{document}
\noindent\begin{minipage}[t]{0.5\textwidth}
\begin{tikzpicture}[baseline]
\coordinate (start) at (0,0);
\slap{1}{\oneline}
\slap{2}{\twolines}
\slap{3}{\twolines}
\slap{4}{\oneline}
\slap{5}{\oneline}
\slap{6}{\oneline}
\slap{7}{\oneline}
\slap{8}{\oneline}
\slap{9}{\oneline}
\slap{10}{\oneline}
\end{tikzpicture}
\end{minipage}%
\begin{minipage}[t]{0.5\textwidth}
\begin{tikzpicture}[baseline]
\coordinate (start) at (0,0);
\slap{1}{\twolines}
\slap{2}{\twolines}
\slap{3}{\twolines}
\slap{4}{\twolines}
\slap{5}{\twolines}
\slap{6}{\oneline}
\slap{7}{\oneline}
\slap{8}{\twolines{} And if I wrote too much, it'll overlap: \twolines}% error here
\slap{9}{\oneline}
\slap{10}{\oneline}
\end{tikzpicture}
\end{minipage}%
\end{document}
这将因线过长而产生错误:
! LaTeX Error: Too many lines here.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.50 ... wrote too much, it'll overlap: \twolines}
% error here
?
答案2
\documentclass{article}
\usepackage{xcolor}
\newenvironment{twolines}
{\noindent
\setbox0=\vbox\bgroup\noindent\begin{minipage}[t]{\textwidth}}
{\strut\end{minipage}\egroup%
\ifdim\dimexpr\ht0+\dp0>2\baselineskip
\textcolor{red}{ERROR: $>$2 lines\\Text Discarded}\else
\setbox2=\vbox{\noindent
\begin{minipage}[t][2\baselineskip][t]{\textwidth}
\box0
\end{minipage}}%
\box2\fi}
\begin{document}
Indents and margins\\are here
\begin{twolines}
1. This is a test
\end{twolines}
\begin{twolines}
2. This is a test\\and more of a test
\end{twolines}
\begin{twolines}
3. This is a test\\and more of a test\\and yet again
\end{twolines}
Next
\end{document}
如果仍然想呈现过长的内容,还有另一种选择:
\documentclass{article}
\usepackage{xcolor}
\newenvironment{twolines}
{\noindent
\setbox0=\vbox\bgroup\noindent\begin{minipage}[t]{\textwidth}}
{\strut\end{minipage}\egroup%
\ifdim\dimexpr\ht0+\dp0>2\baselineskip
\textcolor{red}{WARNING: What follows is $>$2 lines}\\\box0\else
\setbox2=\vbox{\noindent
\begin{minipage}[t][2\baselineskip][t]{\textwidth}
\box0
\end{minipage}}%
\box2\fi
}
\begin{document}
Indents and margins\\are here
\begin{twolines}
1. This is a test
\end{twolines}
\begin{twolines}
2. This is a test\\and more of a test
\end{twolines}
\begin{twolines}
3. This is a test\\and more of a test\\and yet again
\end{twolines}
Next
\end{document}
答案3
您可以使用原语来计算前一段由多少行组成\prevgraf
。
\documentclass[twocolumn]{article}
\newenvironment{twolines}
{\par}
{\par\vspace{\numexpr2-\prevgraf\relax\baselineskip}}
\begin{document}
\begin{twolines}
1. This has one line
\end{twolines}
\begin{twolines}
2. This has one line
\end{twolines}
\begin{twolines}
3. This has two lines, not more than two line, really no more than that
\end{twolines}
\begin{twolines}
4. This has two lines, not more than two line, really no more than that
\end{twolines}
\begin{twolines}
5. This has one line
\end{twolines}
\newpage % just to show what happens
\begin{twolines}
6. This has two lines, not more than two line, really no more than that
but this actually has more than two lines, oops
\end{twolines}
\begin{twolines}
7. This has one line
\end{twolines}
\begin{twolines}
8. This has two lines, not more than two line, really no more than that
\end{twolines}
\end{document}
如果您希望在段落超过两行时发出警告,可以使用此修改版本
\newenvironment{twolines}
{\par}
{\par\ifnum\prevgraf>2 \typeout{WARNING! More than two lines on line \the\inputlineno!}\fi
\vspace{\numexpr2-\prevgraf\relax\baselineskip}}
将记录
WARNING! More than two lines on line 31!