TransWikia.com

How to split the arguments of a command over multiple lines

TeX - LaTeX Asked by Axel Kennedal on March 22, 2021

I’ve defined a newcommand to create a reusable "requirement" component for my LaTeX document. The problem I have is that it seems like I need to have the entire command on a single line, which makes it super-unreadable.

This is how I want to write it:

newcommand{requirement}[5]{
    vspace{1cm}
    largetextsf{textbf{#1}}

    textsc{Type: #2 | Supported by: #3}

    textsf{Description:}

    #4

    textsf{Justification:}

    #5
}

requirement
{R1: Should display available products}
{MVP}
{Interview with client}
{At the front page of the webshop there should be a grid displaying the available products.}
{Users visiting the site need a way to discover products.}

But compiling it gives the error:

! Paragraph ended before text@command was complete.
<to be read again>
                   par
l.1348 {R1:

And currently the only way I know to make it compile is to put everything on one line:

requirement{R1: Should display available products}{MVP}{Interview with client}{At the front page of the webshop there should be a grid displaying the available products.}{Users visiting the site need a way to discover products.}

This example is a short one, some requirements might have hundreds of words, meaning a single line will be ridiculously long. Is there a way to split the parameters to commands over multiple lines?

Edit

For some reason (don’t ask me why) I left out the fact that I’m compiling my document with Pandoc, and by looking at some answers here it seems that Pandoc is what’s causing the "line splitting" to not work. I still have not resolved this though.

3 Answers

Do give

requirement%
{R1: Should display available products}%
{MVP}%
{Interview with client}%
{At the front page of the webshop there should be a grid 
 displaying the available products.}%
{Users visiting the site need a way to discover products.}

While you're at it, do please also change

newcommand{requirement}[5]{

to

newcommand{requirement}[5]{%

A full minimum working example -- note that I've applied a few additions of the basic housekeeping variety to your code -- and its output:

enter image description here

documentclass{article}
newcommand{requirement}[5]{%
begingroup
setlengthparindent{0pt}
    vspace{1cm}
    parnoindent
    {largetextsf{textbf{#1}}par}

    textsc{Type: #2 | Supported by: #3}

    textsf{Description:}

    #4

    textsf{Justification:}

    #5
endgroupparvspace{1cm}
}

begin{document}

requirement
{R1: Should display available products}
{MVP}
{Interview with client}
{At the front page of the webshop there should be a grid displaying the available products.}
{Users visiting the site need a way to discover products.}

end{document}

Answered by Mico on March 22, 2021

The problem is, that you had a completely blank line inside one of the arguments or between two arguments. A completely blank line (not counting spaces) turns into a par. A single newline just turns into a space, so is no problem for your arguments (spaces between normal arguments are ignored).

So, you can split them across lines just fine, e.g., with:

requirement
{R1: Should display available products}
{MVP}
{Interview with client}
{At the front page of the webshop there should be a grid displaying the available products.}
{Users visiting the site need a way to discover products.}

The following would be fine as well:

requirement
  {R1: Should display available products}
  {MVP}
  {Interview with client}
  {%
    At the front page of the webshop there should be a grid displaying the
    available products.%
  }
  {Users visiting the site need a way to discover products.}

(note that I used a % to remove the space at the start and end of the fourth argument, which would be caused by the newline)

And you can ignore an otherwise blank line by putting a % in it, so the following works as well, as there are no implicit par tokens (the % doesn't have to be the first thing in the line, spaces at the start of a line are ignored, just putting % in the line removes the par):

requirement
  %
  {R1: Should display available products}
  %
  {MVP}
  %
  {Interview with client}
  %
  {%
    At the front page of the webshop there should be a grid displaying the
    available products.%
  }
  %
  {Users visiting the site need a way to discover products.}

Please note that your usage of large in the code you provided is wrong, large has to be put into a scope if you don't want to turn the rest of the current group/document into large. Also, I'd use explicit par tokens in the definition of a macro, instead of two consecutive newlines:

newcommand{requirement}[5]{%
    vspace{1cm}%
    {largetextsf{textbf{#1}}par}%
    textsc{Type: #2 | Supported by: #3}par
    textsf{Description:}par
    #4par
    textsf{Justification:}par
    #5par
}

You might want to consider using a description environment for your output. The following uses enumitem to customise the used description environment.

documentclass[]{article}

usepackage{enumitem}

newcommandrequirement[5]
  {%
    vspace{1cm}%
    {largesffamilybfseries#1par}%
    textsc{Type: #2 | Supported by: #3}par
    begin{description}[font=normalfontsffamily,nosep,style=nextline]
      item[Description:] #4
      item[Justification:] #5
    end{description}%
  }

begin{document}

requirement
  {R1: Should display available products}
  {MVP}
  {Interview with client}
  {%
    At the front page of the webshop there should be a grid displaying the
    available products.%
  }
  {Users visiting the site need a way to discover products.}

end{document}

enter image description here

Answered by Skillmon on March 22, 2021

There is no way your code can produce the error. The error can be reproduced almost the same by typing

requirement

{R1: Should display available products}
{MVP}
{Interview with client}
{At the front page of the webshop there should be a grid displaying the available products.}
{Users visiting the site need a way to discover products.}

On the other hand, your code is weak under many respects.

  1. You're declaring large which will affect the whole document from the first call of requirement on (bounded only by a group end).

  2. All paragraphs are affected by the standard indentation; maybe you're setting parindent to zero, but it would be better to state noindent anyway.

  3. vspace is best issued between paragraphs; however, you should use addvspace, so this wouldn't add to other vertical spaces defined with the same addvspace macro (for instance after a section title).

  4. Blank lines inside a definition can be used, but they make par and you don't want page breaks in the middle of a requirement.

Here's a fixed version. You can type each argument in a new line, but leave no blank lines between arguments. Leaving a couple of spaces in front of each argument is, in my opinion, better, in order to ease reading the code.

I don't find 1cm a good spacing, where bigskipamount would be probably better.

documentclass{article}

newcommand{requirement}[5]{%
  par% <--- very important
  addvspace{1cm}% better than vspace in this case
  noindent{largetextsf{textbf{#1}}par}nopagebreak
  noindenttextsc{Type: #2 | Supported by: #3}*
  textsf{Description:}*
  #4*
  textsf{Justification:}*
  #5par
}

begin{document}

requirement
  {R1: Should display available products}
  {MVP}
  {Interview with client}
  {At the front page of the webshop there should be a grid displaying the available products.}
  {Users visiting the site need a way to discover products.}

end{document}

enter image description here

Answered by egreg on March 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP