szhorvat / MaTeX

LaTeX labels in Mathematica
http://szhorvat.net/mathematica/MaTeX
MIT License
375 stars 45 forks source link

The produced .tex file does not compile on 1.5 and 1.6 under Windows 10 #8

Closed lucainnocenti closed 8 years ago

lucainnocenti commented 8 years ago

I see you are still working on this, but this just to warn you, in case you didn't notice, that with the last updates MaTeX is not working anymore on (my?) Windows 10 + MMA v10.4. Even a simple MaTeX[123] produces a .tex file that does not compile. Here is the produced .tex file as an example:

\documentclass[12pt, border=1pt, multi]{standalone}

\newenvironment{matex}{\ignorespaces}{\ignorespacesafterend}

\standaloneenv{matex}

\newbox\MaTeXbox

\newcommand{\MaTeX}[1]{

    \begin{matex}

        \setbox\MaTeXbox\hbox{%

            \strut%

            \(%

                \displaystyle%

                #1%

            \)}%

        \typeout{MATEXDEPTH:\the\dp\MaTeXbox}%

        \typeout{MATEXHEIGHT:\the\ht\MaTeXbox}%

        \typeout{MATEXWIDTH:\the\wd\MaTeXbox}%

        \unhbox\MaTeXbox%

    \end{matex}

}

\usepackage[utf8]{inputenc}

\usepackage{lmodern,exscale}
\usepackage{amsmath,amssymb}

\begin{document}

\fontsize{12pt}{14.4pt}\selectfont

\MaTeX{123}

\end{document}

Trying to compile this gives me the errors:

C:\Users\test\test.tex:46: Missing $ inserted. [\MaTeX{123}]
C:\Users\test\test.tex:46: Missing $ inserted. [\MaTeX{123}]
C:\Users\test\test.tex:46: Missing $ inserted. [\MaTeX{123}]
C:\Users\test\test.tex:46: LaTeX Error: Bad math environment delimiter. [\MaTeX{123}]

I'm not really that expert in tex and I'm not sure how \strut exactly works, but the problem seems to be caused by it

Reverting MaTeX back to the version 1.4.0 makes it work again just fine, so it doesn't look like a problem in my tex configuration.

szhorvat commented 8 years ago

Hi Luca,

Thank you for the warning.

MaTeX 1.6.0 is not released yet. I was planning to release it tomorrow. I'll make sure to resolve this before the final release.

Can you let me know the following:

I'll get the chance to test on Windows later today. On OS X and Linux everything seems to work fine.

lucainnocenti commented 8 years ago

EDIT 2

All right, I think I got it. For some reason FileTemplate adds carriage returns at each new line (which would make sense on windows, except for the fact the Mathematica itself creates newlines both for \r and for \n). However, Importing the template.tex file with the option Text does not add the carriage returns, so that substituting this line with the following one:

template = StringTemplate@Import[FileNameJoin[{DirectoryName[$InputFileName], "template.tex"}], "Text"];

fixes the problem. This (highly unpredictable) issue was probably introduced in this commit when you moved from using StringTemplate to using FileTemplate.

Original comment

Hi szhorvat,

I use pdflatex, from the MiKTeX distribution.

All the empty lines (both those in the previous comments and those in the following code) are in the .tex file, which I copy-pasted here without modifications.

I actually didn't notice the existence of that option, so I just added the following line just before the temporary tex file is created:

Export["C:\\Users\\test\\test.tex", template[content], "Text", CharacterEncoding -> "UTF-8"];

I used the same options used to create the temporary file to obtain the same output. Using MaTeX["123", "TeXFileFunction" -> Print] I get the (seemingly?) same output, except for more empty lines:

\documentclass[12pt, border=1pt, multi]{standalone}

\newenvironment{matex}{\ignorespaces}{\ignorespacesafterend}

\standaloneenv{matex}

\newbox\MaTeXbox

\newcommand{\MaTeX}[1]{

    \begin{matex}

        \setbox\MaTeXbox\hbox{%

            \strut%

            \(%

                \displaystyle%

                #1%

            \)}%

        \typeout{MATEXDEPTH:\the\dp\MaTeXbox}%

        \typeout{MATEXHEIGHT:\the\ht\MaTeXbox}%

        \typeout{MATEXWIDTH:\the\wd\MaTeXbox}%

        \unhbox\MaTeXbox%

    \end{matex}

}

\usepackage[utf8]{inputenc}

\usepackage{lmodern,exscale}

\usepackage{amsmath,amssymb}

\begin{document}

\fontsize{12pt}{14.4pt}\selectfont

\MaTeX{123}

\end{document}

The new empty lines seem to be just due to using Print@Import[file, "String"], as I get them also if I reImport and Print in Mathematica the output file showed in the previous comment.

Does this tex file compile to you? If not, what is the tex file that you are getting?

EDIT

Ok, so I confirm that the produce .tex file works if the empty lines are removed, so that is the source of the problem. Printing template seems to show that that is where the empty lines come from: the FullForm of template is

TemplateObject[List["\\documentclass[12pt, border=1pt, multi]{standalone}\r\n\\newenvironment{matex}{\\ignorespaces}{\\ignorespacesafterend}\r\n\\standaloneenv{matex}\r\n\\newbox\\MaTeXbox\r\n\\newcommand{\\MaTeX}[1]{\r\n\t\\begin{matex}\r\n\t\t\\setbox\\MaTeXbox\\hbox{%\r\n\t\t\t",TemplateSlot["strut"],"%\r\n\t\t\t\\(%\r\n\t\t\t\t",TemplateSlot["display"],"%\r\n\t\t\t\t#1%\r\n\t\t\t\\)}%\r\n\t\t\\typeout{MATEXDEPTH:\\the\\dp\\MaTeXbox}%\r\n\t\t\\typeout{MATEXHEIGHT:\\the\\ht\\MaTeXbox}%\r\n\t\t\\typeout{MATEXWIDTH:\\the\\wd\\MaTeXbox}%\r\n\t\t\\unhbox\\MaTeXbox%\r\n\t\\end{matex}\r\n}\r\n\\usepackage[utf8]{inputenc}\r\n",TemplateSlot["preamble"],"\r\n\\begin{document}\r\n\\fontsize{",TemplateSlot["fontsize"],"pt}{",TemplateSlot["skipsize"],"pt}\\selectfont\r\n",TemplateSlot["tex"],"\r\n\\end{document}\r\n"],Rule[InsertionFunction,TextString],Rule[CombinerFunction,StringJoin]]

I think the problem is caused by all the added \rs. Mathematica interprets both \r and \n as newlines, as seen by evaluating for example "a\r\nb", which generates two empty lines between a and b. I never used FileTemplate and I don't see this in its documentation, but maybe there is some option to suppress this behaviour?

szhorvat commented 8 years ago

Thanks for checking this. Which version of Mathematica are you using? Can you post {$Version, $ReleaseNumber}?

I know you have 10.4, but I need to know if it's 10.4.0 or 10.4.1 and whether it's 32-bit or 64-bit.

Also, just in case: what language/locale is your Windows set to? US English? Italian?

If FileTemplate is buggy then all these details could be relevant.

Finally, you said you used 1.4.0 before. Then you haven't tried 1.5.0 at all? I added FileTemplate in version 1.5.

lucainnocenti commented 8 years ago

Thank you for the great tool!

Version and operative system are {"10.4.0 for Microsoft Windows (64-bit) (February 26, 2016)", 0}.

The language (the one shown in the Time&Language settings) of my Windows is UK English, while the keyboard language is US english (though I don't think the keyboard language has any relevance on this). Windows can be quite funny about the language settings, though, so that there are some random things in the system that still show up in Italian, due to some obscure and hidden language setting that seems to be still set up in italian, and that I didn't really ever bothered to track down.

I hadn't use the 1.5.0 before, but checking it out I see that the problem was also present in that version.

szhorvat commented 8 years ago

Thanks again! One more question:

I develop MaTeX on OS X, so there is no \r\n in template.tex, only \n. How did the \r get into yours? Did you edit the template? Or did you check MaTeX out using git and perhaps git auto-converted the line endings to Windows format?

Either way, it is not acceptable for template.tex not to work with CR-LF line endings, so this will be fixed.

I looked at the source of FileTemplate and it appears to use BinaryReadList to read the file, which explains why it retains the \r characters. I will report this to Wolfram once I get the chance to test everything on Windows.

lucainnocenti commented 8 years ago

Yes, I believe, and I checked with an hex editor, that it is git that automatically handles line endings based on the operative system (see e.g. the relevant page on github.com). Interestingly, there is a way to override this by using a .gitattributes file to enforce a specific line-ending behaviour.

Also, on a completely different note: do you think it would be a problem to change the directory structure of the project to have the MaTeX.m file on the root folder? I find it handy to directly clone the git repository in the Applications folder, but unfortunately Mathematica does not see .m files in subfolders of Applications (but this is an easily fixable matter that may only be relevant to me of course, so if you prefer the current form just ignore this remark!).

szhorvat commented 8 years ago

Fixed by 1641161676bf2a982a6d3e7be8f17b5b62e71886

szhorvat commented 8 years ago

1.6.0 is released now. It also has CR/LF and encoding related fixes to error reporting on Windows.