NIU Department of Mathematical Sciences
Line Breaks in Text Files

Unfortunately, the world is suddenly of the opinion that line breaks are bad. In many cases this is a stupid thing. Ignoring line breaks obviously messes things up in computer code, including TeX/LaTeX. Consider what happens when an email program arbitrarily changes

Important text  % comment
Some more important text
to
Important text  % comment Some more important text
in your TeX file... I see this happening more and more, particularly with some Web-based email services.

Some Unix utilities have a limit of 1024 or so characters per line, so they won't work well with such files. If you run into a situation when - say - vi gives you an error "line too long", don't do :wq out of habit, because the file will be truncated to the portion that vi's buffer managed to read! Instead quit with :q! and open the file with an editor that doesn't have this limitation (vim, nedit, etc.)

When I prepare a TEX file at home using PC and then forward it to the UNIX in my office, all the lines become very long. Although it still work, it does not look good. Is there any automatic way to make line breaks at home with the MILTEX window (I did not find such options) or reformat the file in the UNIX so that the lines become shorter?

I don't know how to make MikTeX's editor "hard wrap" text, like vi is set to do on our system. There are some other editors for Windows that can do that, but you'd have to install one and tell MikTeX to use it. Of course you could also just press return at the end of each line when you type, but that's a hassle.

Some solutions:

To split such lines in a file on our system from within vi (if that's the editor you use):

When you have the file open, type colon (which makes the cursor jump to the command line at the bottom of the screen). Next type 1,$ (this means `apply the command to lines 1 through last'). Now type exclamation mark (this means run an external Unix command on those lines). Finally type fmt -s (that's a Unix command for breaking up lines so they are about 78 characters long).

It sounds complicated, but isn't - your screen should simply have this on the bottom:

  :1,$ ! fmt -s
Then hit return. See if it does what you want. You can always hit "u" right away to undo it, or :q! to quit without saving. Don't forget the -s option in fmt or else some short lines might get joined, and that's what we want to avoid!)

Most other editors can do the same thing (run a Unix command on the file). If you don't want to use an editor, you can simply do the following in the command shell, except you won't see the result on the screen right away:

  fmt -s thefile.tex > thefile.new
  mv thefile.new thefile.tex

Non-Unix line breaks

On top of the "soft wrap" problem there is the difference in how line breaks are represented in various systems. On Unix, text files have lines ending with a "linefeed" character. PC text files use not only linefeed but also carriage return characters, which show up in our editors as ^M. Older Macs in turn use only the carriage return.

The extra carriage returns in PC files are ignored by most programs (e.g. TeX), but can cause problems with others (some compilers). If you transfer a file from a PC to our system and see ^M at the end of each line, it is a good idea to run

dos2unix myfile myfile
on it to convert it. And vice versa, after you are done with it here, run
unix2dos myfile myfile
to change the linebreaks to what PCs like. Note that this is normally not necessary if you use the SSH file transfer program - it is smart enough to do the text file conversion automatically for you.

If you have an older Mac text file on our system, and it shows up in the editor as one long line with ^M (carriage returns) in the text, you can replace all carriage returns with Unix linefeeds with this command:

tr '\r' '\n' < macfile > unixfile
and conversely:
tr '\n' '\r' < unixfile > macfile