Let's say you have a few pages worth of text then numbers, ie name address phone number.

Example :

Name address 0123 4444 name address 0987 6754 name address 5674 5643 etc

How can one create a rule that moves each new 'name address number' to a new line so every one is on a new line?

Eg " if any 4 numbers in a row, then move the next letter to a new line" would do the trick but I am not sure what program it would need to be put in to do it, Word and Excel don't seem to have that functionality .

I'm not a coder but if anyone has any suggestions it would be greatly appreciated.


  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Dec 5 '14 at 7:50
  • 1
    We will need more detail, for example how do you want your input to look? The obvious approach is just to put each entry into an \mbox. – Joseph Wright Dec 5 '14 at 9:06
  • It sounds like you just want a blank line (new paragraph) before each Name – David Carlisle Dec 5 '14 at 9:08
  • Yes a new paragraph for each name would be ok but it's all together so I am not sure how it would distinguish between the name/address. (Provided the text to the right of the name plus the first phone number to the right of that is all on one line. Not sure what the mbox is – user67478 Dec 5 '14 at 9:16
  • For example: name surname address 08 4321 4321 name address 0456 432 321 name surname address 08 4321 4321 name address 0456 432 321 name surname address 08 4321 4321 name address 0456 432 321, is all moved so the "name address number" is on its its own line and the next "name address number" is on the next line down. Where the name, address an number are all different. – user67478 Dec 5 '14 at 9:20

You can try the following macro:

\n addresses with phone numbers \par

The \par (or empty line) signalizes the end of the list of addresses and phone numbers. The macro detect the four digits after the space as the begin of the phone number. Then it prints next four digits (four plus four is supposed as whole phone number). Then the paragraph is ended and next paragraph begins with the next address.

\def\n#1 {#1 \tmpnum=0 \def\nn{}\futurelet\next\nA}
\def\nA{\ifx\next\par \else
  \isdigit\next\iftrue \let\next=\nB \else \nn\let\next=\n \fi
  \expandafter \next \fi
\def\nB #1{\edef\nn{\nn#1}\advance\tmpnum by1
  \ifnum\tmpnum<4 \expandafter\nC \else \expandafter\nD \fi
\def\nD #1#2#3#4{\nn\ #1#2#3#4\par\n}
      \ifnum\expandafter`\tmp<`0 \digitfalse \else 
      \ifnum\expandafter`\tmp>`9 \digitfalse \else \digittrue \fi\fi
   \else \digitfalse \fi
\def\isdigitA#1 #2 #3{#3}

name addres 12 1234 5678 other name address 13 3456 7890 another 123 address
8765 4321


enter image description here

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.