Doc merge prints blank rows

Under normal Sugar logic, each product included as a quoted line item possesses an assigned product category. In our document merge template, we aim to include products meeting specified conditions. However, the resulting PDF document presents several empty rows of data interspersed. Can anyone provide suggestions for eliminating empty rows?

{#products max_num='5’}
{if cattest_c=='Category1'} 
{product_template_name.name capitalize='true'}
{elseif cattest_c=='Category2'}{quantity}  {product_template_name.name capitalize='true'}
{endif}
{/products}