How to add a line break after every period in a .txt file

149 views Asked by At

I want to add line break after every .?! but not in decimals. However, I'm not very skilled at programming and all that (I only learned really basic python).

So I wanted to know if there's a way to do this (in an easy way, if possible) or what I have to do to make this line code work. It doesn't necessarily needs to work if the .txt directly, If I can make it work with copying and pasting it's alright.

I googled it and found this: https://tex.stackexchange.com/a/558403

\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function dot2newline ( s )
   s = s:gsub ( "([%.%?!])%s+" , "%1\\newline " ) 
   s = s:gsub ( "([%.%?!])$"   , "%1\\newline " )
   return s
end
\end{luacode}
\AtBeginDocument{\directlua{ luatexbase.add_to_callback (
   "process_input_buffer" , dot2newline , "dot2newline" )}}
 
\begin{document}
\setlength\parindent{0pt} % just for this example

Hello. Hello? Hello! $0.0<1.0$.  3.14159. \texttt{tex.stackexchange.com}. 
... ...
Hi!
Where?
C.\ Ph.\ E.\ Bach
\end{document}

Then, I replace the text with a part of the text that I want. However, it shows this:

Underfull \hbox (badness 10000) in paragraph at lines

1

There are 1 answers

0
Hermann12 On

Search for a list of character and add a line break:

import re

x = """Hello. Hello? Hello! C.\ Ph.\ E.\ Bach 2.456”""
patterns = ['. ', '? ', '! ']

for pattern in patterns:
    x = x.replace(pattern, pattern+'\n')

print(x)

Output:

Hello. 
Hello? 
Hello! 
C.\ Ph.\ E.\ Bach 2.456