12

I have a number of files (Jupyter notebooks, .ipynb) which are text files. All of these contain some LaTeX markup. But when I run file, I get:

$ file nb_*          
nb_1.ipynb:      ASCII text
nb_2.ipynb:      ASCII text
nb_3.ipynb:      ASCII text, with very long lines
nb_4.ipynb:      LaTeX document, ASCII text, with very long lines
nb_5.ipynb:      text, with very long lines

How does file distinguish these? I would like all files to have the same type.


(Why should the files have the same type? I am uploading them to an online system for sharing. The system classifies them somehow and treats them differently, with no possibility for me to change this. I suspect the platform uses file or maybe libmagic internally and would like to work around this.)

New contributor
cheersmate is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 1
    Have you tried the --keep-going option? – ctrl-alt-delor Feb 13 at 18:36
  • 2
    You can find the rules in, for example,/usr/share/file/magic/*; the actual location may vary depending on how file was packaged and/or installed. – chepner Feb 13 at 18:52
27

The file type recognition is driven by so-called magic patterns. The magic file for analyzing TeX family source code contains a number of macro names that cause a file to be classified as LaTeX. Each match is assigned a strength, e. g. 15 in case of \begin and 18 for \chapter. This makes the heuristic more robust against false positives like misclassification of Plain TeX or ConTeXt documents that happen to define their own macros with those names.

  • 2
    grep -i latex /usr/share/misc/magic gets the specific patterns. Your magic file location may vary, of course. @cheersmate, you may be able to find a particular pattern or two in the "LaTeX" doc. Similarly, look for non-ASCII characters/bytes in the non-ASCII file (if it makes a difference). – mpez0 Feb 14 at 14:32
2

I found one string which seems to make file classify a file as LaTeX:

$ cat text
a
b
$ cat latex
a
\begin
b
$ file text latex
text:  ASCII text
latex: LaTeX document, ASCII text

So at least I can force all files to have the same type by adding some environments to the files currently classified as text.

New contributor
cheersmate is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

cheersmate is a new contributor. Be nice, and check out our Code of Conduct.

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.