gpoore / minted

minted is a LaTeX package that provides syntax highlighting using the Pygments library. Highlighted source code can be customized using fancyvrb.
1.73k stars 125 forks source link

[Feature request] Verbosity flag that displays where (in the original source) a `.pyg` was created from #348

Open whisperity opened 1 year ago

whisperity commented 1 year ago

I have had a run-in with a rather arcane error when trying to build my PhD thesis on a newer machine. I got an error Command ttfamily invalid in math mode and the error pointed to one of the files generated by minted. Unfortunately, what followed was 1-1.5 hours of manual reading, flag tinkering, rebuilding, trying and failing, and then combinatorically commenting out chapters and then sections trying to zero in the location which is of a human-readable size. (The document is very complex, I have several depths of inclusions, dependencies on build flags that build a part in this or that language, etc.)

Neither frozencache and its sequencing, nor trying to read the contents of the generated file was meaningful, my papers repeat similar words and few word sequences over and over again in multiple locations. And because the entire work is very long, there were no pointers originally that could be gathered even from the context (as even the frozencache is it seems uniqued and does not repeat even if the original text includes the same sequence again after one another).

After finding the issue, the fix was rather trivial:

\begin{figure} 
  % ...
  \caption{Foo $X_{bar}(\mintinline{C}{fun()})$}
\end{figure}

had to be replaced:

 \begin{figure} 
   % ...
-  \caption{Foo $X_{bar}(\mintinline{C}{fun()})$}
+  \caption{Foo $X_{bar}\left(\right.$\mintinline{C}{fun()}$\left.\right)$}
 \end{figure}

I wholeheartedly understand that the original mistake was my doing (although I have no idea why my older computer (and a latex-action CI script that I'm using on GitHub) kept accepting it, but I must have upgraded across TeXLive, LaTeX engine, Python interpreter, pigments library, minted version, etc. boundaries together...) so this is more of a comment than a question (:wink:), could we please have some sort of trivial flag we can throw at minted (like trace or location-debug or something?) which would make it try putting as accurate as possible location markers into the generated code? Neither a sequenced numbering nor a seemingly random hash is helpful to map the generated code back into the original document. It would be enough if it did not appear inside the rendered PDF itself, but only as a comment in the .pyg files.

muzimuzhi commented 1 year ago

I got an error Command ttfamily invalid in math mode and the error pointed to one of the files generated by minted.

\mintinline will call \ttfamily internally (resulted of the default setting fontfamily=tt in fancyvrb package), which is a text font switching command hence is invalid in math mode. You can wrap the whole \mintinline in a amsmath \text command:

\documentclass{article}
\usepackage{amsmath}
\usepackage{minted}

\begin{document}
Foo $X_{bar}\text{\mintinline{C}{fun()}}$
\end{document}

so this is more of a comment than a question (😉), could we please have some sort of trivial flag we can throw at minted (like trace or location-debug or something?) which would make it try putting as accurate as possible location markers into the generated code?

Unfortunately, the error is thrown by LaTeX (when .pygtex is inputted), hence there's not many things the python layer of minted can do. Perhaps minted can check if it starts in math mode?

From another side, TeX does have some sort of file tracing, so a "smart" editor should be able to filter the corresponding file name from log file. And the log is also human readable:

(./_minted-texstudio_FcTlhz/69E6A439C52DDA41066D8EBAAE241E9E0F4BDC15A1675A8F5A8
07DB4E50454FC.pygtex

! LaTeX Error: Command \ttfamily invalid in math mode.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...rn>  for immediate help\@err@                                              

l.3 \end{Verbatim}

Please use the math alphabet \mathtt instead of the \ttfamily command. 

)

Update: My bad, this doesn't link .pygtex to the position of \mintinline in .tex.