Mathematica Asked by Pace Nielsen on November 15, 2020
Say I have a file named “Silly.tex” which has rn 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
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, "rn" -> "n"]
, then re-export as "String". This method should work on all platforms.
Correct answer by Szabolcs on November 15, 2020
You may try this:
s = Import["Silly.tex", "Text"];
s = StringReplace[s, "rn" -> "n"];
Export["Silly_new.tex", s, "Text"]
Answered by Henrik Schumacher on November 15, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP