3
$\begingroup$

Say I have a file named "Silly.tex" which has \r\n line breaks. Is there a way to get Mathematica to convert it to only have \n line breaks?


You can find such a file with DOS line breaks at: https://math.byu.edu/~pace/Silly.tex

$\endgroup$
4
$\begingroup$

There are two "text" formats that work with Import/Export, namely "String" and "Text".

"String" will read/write the bytes in the file as they are. It does not try to interpret those bytes as text at all. It just puts them into a String data structure as they are.

"Text" will interpret the bytes as text. What does that mean? Well, a "text file" may be many different things. There are several different conventions about how text can be represented by bytes (numbers) in a file. The most important thing is the character encoding. It can be set for "Text" import. It can be ASCII, UTF-8 or something else. Then there's a convention about line endings. "Text" import interprets all of these, while "String" import does not.

As documented, "Text" import will replace all common line ending markers by \n on import (i.e. you'll get an \n in the string, no matter what was in the file). On export, the ending convention of the operating system is used.

What this means is that if you are on macOS or Linux, you need to import as "Text", after setting the CharacterEncoding if necessary (in practice: if you have any accented or non-Latin characters), then just re-export as "Text" again. The line endings will be Unix style.

If you are on Windows, this will not work, as the exported result will always use DOS style line endings.

When you can do then is to import as "String", do the string replace that Henrik suggested, StringReplace[s, "\r\n" -> "\n"], then re-export as "String". This method should work on all platforms.

$\endgroup$
  • 1
    $\begingroup$ It didn't work the first time I tried it. But then I realized that "\n\r" should instead be "\r\n"! With that additional change, it worked. $\endgroup$ – Pace Nielsen Nov 27 '18 at 23:13
3
$\begingroup$

You may try this:

s = Import["Silly.tex", "Text"];
s = StringReplace[s, "\n\r" -> "\n"];
Export["Silly_new.tex", s, "Text"]
$\endgroup$
  • $\begingroup$ @PaceNielsen Did it work? $\endgroup$ – Henrik Schumacher Nov 27 '18 at 9:57
  • $\begingroup$ It did not work. Apparently Mathematica infers a format type for ".tex" files, and replaced the contents with: %% AMS-LaTeX Created with the Wolfram Language : www.wolfram.com \documentclass{article} \usepackage{amsmath, amssymb, graphics, setspace} \newcommand{\mathsym}[1]{{}} \newcommand{\unicode}[1]{{}} \newcounter{mathematicapage} \begin{document} [\text{Text}] \end{document} $\endgroup$ – Pace Nielsen Nov 27 '18 at 15:49
  • $\begingroup$ When I replaced .tex with something nonsensical like .txx it also doesn't work, saying "Export::infer: Cannot infer format of file M4N13R-511.txx." $\endgroup$ – Pace Nielsen Nov 27 '18 at 15:51
  • 1
    $\begingroup$ @PaceNielsen Oops. I forgot to feed the actual string into the Export command. Please try again. (Things like this happen easily if the poster of a question does not include a minimal example for testing...) $\endgroup$ – Henrik Schumacher Nov 27 '18 at 15:56
  • $\begingroup$ It still isn't working. (The line breaks are not changed.) I've now included a link to such a file with DOS line breaks, if that helps you. $\endgroup$ – Pace Nielsen Nov 27 '18 at 21:34

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.