Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax highlighting issue when using custom environments that should print verbatim #2789

Closed
medwatt opened this issue Sep 11, 2023 · 5 comments
Labels

Comments

@medwatt
Copy link

medwatt commented Sep 11, 2023

Description

I am using minted in LaTex to provide syntax highlighting to text inside the code block below. The language I am trying to highlight has functions of the form $function_name. The issue here is that $ by itself is considered the beginning of a mathematical expression. As a result, everything else that follows is considered part of the mathematical expression until a closing $ is found, which could be at the end of the document. In short, I lose proper syntax highlighting within vim and this messes up all my snippets since they are context-specific.

\begin{verilog}{}
    $strobe("Average period = %g measured from %d periods.", period, total);
\end{verilog}

One half-solution is to augment a \ before every $ sign. However, because minted prints out verbatim, the \ also gets printed, which is not supposed to.

I've noticed that vimtex does recognize some specific environments like minted and and highlights the block accordingly. How can we make our custom environments behave in the same way?

Steps to reproduce

Use an environment with a custom name:

\begin{verilog}{}
    $strobe("Average period = %g measured from %d periods.", period, total);
\end{verilog}

Expected behavior

The content of the environment should be considered verbatim, not LaTex code.

Actual behavior

The content is considered LaTex code.

Do you use a latexmkrc file?

No

VimtexInfo

System info:
  OS: Manjaro Linux
  Vim version: NVIM v0.9.1
  Has clientserver: true
  Servername: /run/user/1000/nvim.346500.0

VimTeX project: verilog-a
  base: verilog-a.tex
  root: /home/medwatt/Writing/LatexDocuments/beamer/verilog-a
  tex: /home/medwatt/Writing/LatexDocuments/beamer/verilog-a/verilog-a.tex
  main parser: current file verified
  document class: beamer
  packages: amsbsy amsfonts amsgen amsmath amsopn amssymb amstext amsthm atbegshi atbegshi-ltx atveryend atveryend-ltx auxhook beamerbaseauxtemplates beamerbaseboxes beamerbasecolor beamerbasecompatibility beamerbasedecode beamerbasefont beamerbaseframe beamerbaseframecomponents beamerbaseframesize beamerbaselocalstructure beamerbasemisc beamerbasemodes beamerbasenavigation beamerbasenotes beamerbaseoptions beamerbaseoverlay beamerbaserequires beamerbasesection beamerbasetemplates beamerbasethemes beamerbasetheorems beamerbasetitle beamerbasetoc beamerbasetranslator beamerbasetwoscreens beamerbaseverbatim beamercolorthemedefault beamercolorthemeorchid beamercolorthemewhale beamerfontthemedefault beamerinnerthemedefault beamerinnerthemerounded beamerouterthemedefault beamerouterthemeinfolines beamerthemeMadrid beamerthemedefault bigintcalc bitset calc catchfile enumerate enumitem environ etexcmds etoolbox expl3 fancyvrb float fontenc fontspec fontspec-xetex framed fvextra geometry gettitlestring graphics graphicx hycolor hyperref ifluatex ifplatform iftex ifthen ifvtex infwarerr intcalc keyval kvdefinekeys kvoptions kvsetkeys letltxmacro lineno listings lstmisc ltxcmds minted nameref pdfcol pdfescape pdftexcmds pgf pgfcomp-version-0-65 pgfcomp-version-1-18 pgfcore pgffor pgfkeys pgfmath pgfrcs pgfsys refcount rerunfilecheck shellesc stringenc tcolorbox textcomp tikz tikzfill-common tikzfill.image translator trig trimspaces uniquecounter upquote url verbatim xcolor xparse xstring xxcolor
  source files:
    verilog-a.tex
    settings/packages.tex
    settings/macros.tex
  compiler: latexmk
    engine: -xelatex
    options:
      -xelatex
      -verbose
      -shell-escape
      -file-line-error
      -synctex=1
      -interaction=nonstopmode
    callback: 1
    continuous: 1
    executable: latexmk
  viewer: Zathura
    xwin id: 0
  qf method: LaTeX logfile
@medwatt medwatt added the bug label Sep 11, 2023
@lervag
Copy link
Owner

lervag commented Sep 11, 2023

In short, I lose proper syntax highlighting within vim and this messes up all my snippets since they are context-specific.

\begin{verilog}{}
    $strobe("Average period = %g measured from %d periods.", period, total);
\end{verilog}

Can you provide a full (minimal) example; one that I can compile on my end?

I've noticed that vimtex does recognize some specific environments like minted and and highlights the block accordingly. How can we make our custom environments behave in the same way?

It's easier to answer when I fully understand what you are doing, cf. my above question.

@medwatt
Copy link
Author

medwatt commented Sep 11, 2023

\documentclass{article}

\usepackage[most, minted]{tcolorbox}
\usepackage{minted}
\setminted{
    autogobble=true,
    breaklines,
    fontsize=\small,
}

\newtcblisting{verilog}[2][]{%
    listing engine=minted,
    minted language=verilog,
    listing only,
    breakable,
    enhanced,
    colframe=gray!75,
    title=#2,
    coltitle=black,
    colbacktitle=gray!50,
    fonttitle=\bfseries,
    top=0.5mm,
    bottom=0.5mm,
    % left=0mm,
    right=0mm,
    boxsep=0.5mm,
    minted options = {
        linenos,
        breaklines=true,
        breakbefore=.,
        numbersep=2mm,
    },
    overlay={%
        \begin{tcbclipinterior}
            \fill[gray!25] (frame.south west) rectangle ([xshift=4mm]frame.north west);
        \end{tcbclipinterior}
    },
    #1
}

\begin{document}

This is the first line.

\begin{verilog}{}
    $strobe("Average period = %g measured from %d periods.", period, total);
\end{verilog}

This line will be highlighted as part of a mathematical expression.

\end{document}

lervag added a commit that referenced this issue Sep 11, 2023
@lervag
Copy link
Owner

lervag commented Sep 11, 2023

The \newtcblisting command was not supported. I've added minimal support for it now to prevent the bug you mention. This can probably be further improved to allow nested syntax, but that requires more work.

@medwatt
Copy link
Author

medwatt commented Sep 11, 2023

Thanks. I wonder if I could ask you to add support for \mintinline?

For example:

\newcommand{\mi}[1]{\mintinline{verilog}{#1}}

@lervag
Copy link
Owner

lervag commented Sep 11, 2023

There is already support for \mintinline, but of course, there is no support for your custom \mi command. And I won't do that explicitly, sorry. :)

@lervag lervag closed this as completed Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants