我有以下宏
\makeatletter
\newcommand{\requirement}[3]{%
\par\noindent
\begin{minipage}{\linewidth}
\color{blue}%
\leavevmode\ignorespaces#1 \hfill {REQ }\ignorespaces#2\\
\rule[1ex]{\textwidth}{1.0pt}%
\end{minipage}%
\par\nobreak\@afterheading
\noindent\ignorespaces#3
\par\vspace{1cm}
}
\makeatother
我使用它来创建需求列表。宏的最后一行\par\vspace{1cm}
用于在几个连续元素之间创建距离。问题是,如果这样的 a后面requirement
跟着例如 a,而 a\section
本身会插入一个前导空格,则产生的空格太大。我能否以某种方式检查是否必须添加空格,或者它是否是部分、子部分的结尾……以保持统一的外观。
这是 MVE
\documentclass[a4paper,10pt,notitlepage]{article}
\usepackage{xcolor}
\begin{document}
\makeatletter
\newcommand{\requirement}[3]{%
\par\noindent
\begin{minipage}{\linewidth}
\leavevmode\ignorespaces#1 \hfill {REQ }\ignorespaces#2\\
\rule[1ex]{\textwidth}{1.0pt}%
\end{minipage}%
\par\nobreak\@afterheading
\noindent\ignorespaces#3
\par\vspace{1cm}
}
\makeatother
\section{Section}
\subsection{Subsection}
\requirement{A Requirement}{001}{
This is a requirement specification...
}
\requirement{Another Requirement}{002}{
This is another requirement specification...
}
\subsection{A New Subsection}
Some text that has absolutely no meaning
\end{document}
我要怎么做才能在之前有一个“常规”部分间距,REQ 001
并且在之后没有额外的间距REQ 002
以获得统一的外观。
答案1
您需要将问题反过来看,这样更容易找到解决方案。您问的是:“在要求文本之间添加 1cm 的间距。”
- 要求:在块周围留出间距。正如您在 REQ 001 之前所写的那样,留出“常规”部分间距,在 REQ 002 之后不留额外间距,以获得统一的外观。
- 使用拨动开关检测是否使用了“要求”命令。
- 如果使用,则在下次调用时在顶部添加空格。
这是一个粗略的解决方案。
\documentclass[a4paper,10pt,notitlepage]{article}
\usepackage{xcolor}
\makeatletter
\newif\if@toggle
\def\addmyspace{%
\if@toggle
\hrule height 1cm width 5pt %
\else
\fi
}
\NewDocumentEnvironment{requirements}{ }
{
\newcommand{\requirement}[3]{%
\par
\addmyspace
\noindent
##1 \hfill {REQ } ##2\\
\rule[1ex]{\textwidth}{1.0pt}%
\par\nobreak\@afterheading
\noindent\ignorespaces##3
\par
\@toggletrue
}
}
{}
\makeatother
\begin{document}
\section{Section}
\subsection{Subsection}
\begin{requirements}
\requirement{A Requirement}{001}{
ghis is a requirement specificatiog...
}
\requirement{Another Requirement}{002}{
teststss
}
\end{requirements}
\subsection{A New Subsection}
Some text that has absolutely no meaning\\
test
更复杂的解决方案是:
\documentclass[a4paper,10pt]{book}
\usepackage[latin,UKenglish]{babel}
\usepackage{xcolor, lipsum}
\usepackage[colorlinks]{hyperref}
\makeatletter
\newif\if@toggle
\def\addmyspace{%
\if@toggle
\hrule height 1cm width 5pt %
\else
\fi
}
\newcounter{req}
\setcounter{req}{1}
\def\dispreq{\ifnum\thereq<10 %
REQ-00\thereq \else REQ-0\thereq\fi}
\NewDocumentEnvironment{requirements}{ }
{
\newcommand{\requirement}[3]{%
\phantomsection
\par
\addmyspace
\noindent{\sffamily
##1 \hfill \dispreq}\\[-1ex]
\rule{\textwidth}{1.0pt}%
\par\nobreak\@afterheading
\noindent\ignorespaces##3 %
\par
\@toggletrue
\stepcounter{req}
}
}
{}
\makeatother
\begin{document}
\chapter{Test}
\newpage
\section{Section}
\subsection{Subsection}
\begin{requirements}
\requirement{Do not affect spacing around a block of requirements}{}{
This is a requirement specification \label{first}
}
\requirement{Between requirements leave a 1cm space}{}{
\lipsum[1]
}
\requirement{Between requirements leave a 1cm space}{}{
\lipsum[1]
}
\requirement{Allow requirements text to be breakable.}{}{
Must not use \texttt{minipage} see \ref{first}
\lipsum[1-2]
}
\requirement{Blocks should be able to be referenced. There can also be more detailed requirements, as you may need to do some adjustments for color, fonts and other details such as what to do with a longer text on the heading. As you see from this block, it maybe better to use a sans-serif font for the requirement headings.}{}{
Use the hyperref package for this. use \string\phantomsection.
}
\end{requirements}
\subsection{A New Subsection}
Some text that has absolutely no meaning\\
test.
Use maths for specifications where, you can \href{https://lamport.azurewebsites.net/tla/book.html}{Lamport}.
\end{document}
它具有自动编号和一些其他好功能。