2

I have the following macro

\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

I use this to create a list of requirements. The last line of the macro \par\vspace{1cm} is used to create a distance between several sequential elements. The problem is that if such a requirement is followed by e.g. a \section which itself inserts a preceding space, the resulting space is way too big. Can I somehow check whether I have to add the space or if it's the end of a section, subsection... in order to keep a uniform appearance.

Here's a 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}

enter image description here

What would I have to do to have a "regular" section spacing before the REQ 001 and no extra spacing after REQ 002 to get a uniform look.

po.pe
  • 329
  • 1
    Can you please show us a short compilable TeX code resulting in your issue? Then we do not have to guess what you are doing ... – Mensch Sep 20 '21 at 14:32
  • 1
    Using \addvspace instead of \vspace will employ the largest value between the 1cm and the skip before the section, so 1cm (with standard classes). Would this be OK or do you want the space to be the one employed by \section? – campa Sep 20 '21 at 14:51
  • @Mensch, MVE added – po.pe Sep 21 '21 at 05:23
  • @campa This improves the situation, but the spacing is still not the same as if there was just regular text... – po.pe Sep 21 '21 at 05:23
  • You need to turn the problem around, as the way you went about it has conflicting specs. Create an environment, in which you write requirements. Adjust spacing at end of environment and between "requirement headings" – yannisl Sep 23 '21 at 02:49

1 Answers1

1

You need to turn your question around and is easier to see the solution. What you are asking is: "add 1cm space between requirements text."

  1. Requirements: leave spacing around the blocks as is. As you wrote "regular" section spacing before the REQ 001 and no extra spacing after REQ 002 to get a uniform look.
  2. Use a toggle switch to detect if a "requirement" command was used.
  3. If used, on the next call, add space on top.

enter image description here

Here is a rough solution.

\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

A more complex solution is:

\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}

This has automatic numbering and some other goodies.

yannisl
  • 117,160
  • This is great! Is there some online course to learn Latex scripting like this? – po.pe Sep 24 '21 at 12:34
  • I am not aware of any, other than some short youtube videos. Personally a long time user of LaTeX, I learned mostly by doing and peeking at the code of various packages. – yannisl Sep 24 '21 at 13:41
  • The thing with Latex (in my opinion at least) is that is usually pretty difficult to figure out what you're actual looking for what it comes to this level of "scripting" :) – po.pe Sep 25 '21 at 16:12
  • This is true, as it is a different concept that normal programming. Found a link for you https://tex.stackexchange.com/questions/11/what-are-good-learning-resources-for-a-latex-beginner – yannisl Sep 25 '21 at 22:42