# Processing Plain Text

I'm currently trying to typeset a plain text file that has a certain structure. There's one thing that I can't get working, and that is the following. My plain text is structures as follows:

"lorem ipsum dolor sit amet." (A block of text.)

Title

"lorem ipsum dolor sit amet." (A block of text.)


So every section in my document has a title above it (seperated by a "/newline"), and every section is seperated from the other section by two times this.

Basically I'm trying to convert the above to:

"lorem ipsum dolor sit amet." (A block of text.)

\section{Title}
"lorem ipsum dolor sit amet." (A block of text.)


Would it be possible to do something like this?

• this would be ridiculously simple to format with plain tex. do you have any need for any particular latex features, or strong desire to use latex instead of plain tex? more relevant to the content of the file, is there only one paragraph in a section, or could there be more than one, and if more than one, how do you want the paragraph separation to be distinguished? by a blank line, or is an indentation okay? (by the way, welcome to TeX.SX!) – barbara beeton Oct 20 '14 at 20:01
• Hm... I would probably use sed or perl or something like that: perl -0777 -i.bak -pe 's/\n\n\n(.*)\n\n/\n\n\\section{\1}\n/' plain.txt would reformat plain.txt as desired, but keep a plain.bak backup file in case something goes wrong. If you are using Windows line endings, the RegEx may have to be adapted, though. – Daniel Oct 20 '14 at 20:08
• @barbarabeeton Could you not use Plain-style \definitions to insert the necessary \section directives? – Sean Allred Oct 20 '14 at 22:24
• Thanks for the answers Barbara en @daniel. It's basically a diary I'm trying to convert, so you could say every entry is it's own section. I tried your suggestion Daniel, and it worked! – ProcessingText Oct 21 '14 at 10:19
• @SeanAllred -- it seems i jumped to an unwarranted conclusion, that it was the tex processing that was wanted, not the transformation to tex coding. however, it's actually possible to process this without inserting any tex commands, provided none of the text blocks is more than one paragraph. i'll have to research that -- i've actually done something nearly the same, but probably 30 years ago, so a bit of archaeology is involved. – barbara beeton Oct 21 '14 at 12:07

While it is certainly possible (and maybe also fun) to do such transformations in (La)TeX directly, I would just employ perl or a similar tool that supports search & replace across multiple lines. In your case:

perl -0777 -i.bak -pe 's/\n\n\n(.*)\n\n/\n\n\\section{\1}\n/' plain.txt


will transform the file text.txt accordingly.

Some details:

• The -0777 option makes perl read the file at once, which is necessary here to match across multiple lines.

• The file plain.txt will be modified "in place" (-i option), but a backup is created in plain.bak (suffix provided to -i).

• In the regex we search for three consecutive newlines \n\n\n followed by arbitrary text (.*) that is terminated by two newlines \n\n.

• In the replace part we insert two newlines \n\n followed by the \section{...} command and another newline \n. The \1 is substituted with the first bracketed expression of the search regex, which in this case is the matched "arbitrary text".

If I understand your question, you need to interpret every paragraphs (separated by blank line) as a section title if such paragraph preceedes exactly two blank lines. You can try this:

\def\par{\endgraf\futurelet\next\parA}
\def\parA{\ifx\next\par \expandafter\sect\fi}
\long\def\sect#1#2\par{\bigskip{\bf #2}\par\nobreak\medskip}

block of text

section title

first paragraph block of text block of text block of text

second paragraph block of text block of text block of text

sect2 title

next paragraph block of text block of text block of text

\bye


But this code is very fragile. You can try to insert two blank lines before \bye for example and you get an error. If you insert blank lines between macros then you will have new problems, if there are two consecutive blank lines.

With Notepad++, use the extended search mode and look for three return to line you entered (two empty lines) using

\r\n\r\n\r\n


Replace it by

\r\n\r\n\r\n\section{


close the \section command by knowing that \r\n\r\n" follows. Replace

\r\n\r\n"


by

}\r\n"


You will then transform

"lorem ipsum dolor sit amet." (A block of text.)

Title

"lorem ipsum dolor sit amet." (A block of text.)


to:

"lorem ipsum dolor sit amet." (A block of text.)

\section{Title}
"lorem ipsum dolor sit amet." (A block of text.)

• Thanks for the answer alexandre-willame. I'm using a Mac, so I can't install Notepad++, but Daniel's suggestion worked. – ProcessingText Oct 21 '14 at 10:20
• @ProcessingText And a voice whispered in the darkness… "AUCTeX…" – Sean Allred Oct 21 '14 at 12:39