0

I have the following input that I receive from a third-party program:

START OF SECTION    
    This is a long sentence; really long that when rendered by PDFLatex it becomes a paragraph that spans a number of lines. Unfortunately, except the first line, all remaining lines will start from the very left of the document, but I want them to start from the indentation position of the first word of the paragraph.
    START OF SECTION        
        This is a long sentence; really long that when rendered by PDFLatex it becomes a paragraph that spans a number of lines. Unfortunately, except the first line, all remaining lines will start from the very left of the document, but I want them to start from the indentation position of the first word of the paragraph.
        START OF SECTION            
            This is a long sentence; really long that when rendered by PDFLatex it becomes a paragraph that spans a number of lines. Unfortunately, except the first line, all remaining lines will start from the very left of the document, but I want them to start from the indentation position of the first word of the paragraph.
        END OF SECTION
    END OF SECTION
END OF SECTION

I want to generate a PDF that looks exactly like is shown below: The indentation of each paragraph (the text inside the SECTION delimiters) follows the indentation of the first word/line of that paragraph.

enter image description here

I do not produce the text myself, like I said it is fed to me by another tool; so I cannot use enumeration unless I parse it and rewrite it with nested enumeration -- but I am trying to leave this as the very last resort.

Can anybody tell me the magic thing to do in order to instruct Latex to indent every paragraph according to its first line's indentation?

Many thanks.

2

enter image description here

\documentclass{article}

\begin{document}


\parindent0pt
\catcode`\ \active
\def\par{\endgraf\leftskip=0pt\relax}%
\obeylines
\def {\ifvmode\advance\leftskip5pt\relax\else\space\fi}%


START OF SECTION    
    This is a long sentence; really long that when rendered by PDFLatex it becomes a paragraph that spans a number of lines. Unfortunately, except the first line, all remaining lines will start from the very left of the document, but I want them to start from the indentation position of the first word of the paragraph.
    START OF SECTION        
        This is a long sentence; really long that when rendered by PDFLatex it becomes a paragraph that spans a number of lines. Unfortunately, except the first line, all remaining lines will start from the very left of the document, but I want them to start from the indentation position of the first word of the paragraph.
        START OF SECTION            
            This is a long sentence; really long that when rendered by PDFLatex it becomes a paragraph that spans a number of lines. Unfortunately, except the first line, all remaining lines will start from the very left of the document, but I want them to start from the indentation position of the first word of the paragraph.
        END OF SECTION
    END OF SECTION
END OF SECTION
\end{document}

It should work (I think) in RTL.

Vertical mode detects that you are between paragraphs, in that mode the space is not output at all, it is just used to increase the leftskip (oh, you may need rightskip) then once the paragraph starts and you are in horizontal mode a space expands to \space which is a normal space.

Then at the linebreak (end of paragraph here) \leftskip is reset to 0 ready for the next set of spaces to increase it to the value needed for the next paragraph.

4
  • Awesome, works like a charm :-) Many thanks. – Amine Marref Oct 13 '20 at 21:20
  • Could you please explain to me the work of the vertical mode conditional? I need to apply the same formatting for input that is Non-English and RTL, and this otherwise flawless solution is not directly applicable there. – Amine Marref Oct 13 '20 at 21:33
  • I'll add some notes. @AmineMarref – David Carlisle Oct 13 '20 at 21:39
  • Superb, thank you for the concise explanation :-) – Amine Marref Oct 13 '20 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.