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

Make `langlinenos` with with `\inputminted` #361

Closed Witiko closed 10 months ago

Witiko commented 1 year ago

Unline the minted environment and the \mint commands, the \inputminted command does not seem to be affected by the langlinenos option:

\documentclass{article}
\usepackage[langlinenos]{minted}
\setminted{linenos, firstnumber=last}
\begin{document}
\begin{filecontents}[overwrite, nosearch, noheader]{python-01.py}
def f(x):
    return x**2
\end{filecontents}
\inputminted{python}{python-01.py}
\begin{filecontents}[overwrite, nosearch, noheader]{ruby.rb}
def func
    puts "message"
end
\end{filecontents}
\inputminted{ruby}{ruby.rb}
\begin{filecontents}[overwrite, nosearch, noheader]{python-02.py}
def g(x):
    return 2*x
\end{filecontents}
\inputminted{python}{python-02.py}
\end{document}

snip

The documentation does not explicitly mention that the \inputminted should be affected by the langlinenos option, so this is not necessarily a bug. However, the inconsistency is surprising.

muzimuzhi commented 1 year ago

Try this patch

\begin{filecontents}[overwrite, nosearch, noheader]{python-01.py}
# python
def f(x):
    return x**2
\end{filecontents}
\begin{filecontents}[overwrite, nosearch, noheader]{ruby.rb}
# ruby
def func
    puts "message"
end
\end{filecontents}

\documentclass{article}
\usepackage[draft=false, langlinenos]{minted}
\usepackage{xpatch}

\makeatletter
\ifminted@draft
  \xpatchcmd\inputminted
    {\VerbatimInput{#3}}
    {\minted@langlinenoson
     \VerbatimInput{#3}%
     \minted@langlinenosoff}
    {}{\PatchFailed}
\else
  \xpatchcmd\inputminted
    {\minted@pygmentize[#3]{#2}}
    {\minted@langlinenoson
     \minted@pygmentize[#3]{#2}%
     \minted@langlinenosoff}
    {}{\PatchFailed}
\fi
\makeatother

\setminted{linenos, firstnumber=last, autogobble}
\setminted[python]{numbersep=30pt}

\begin{document}
\begin{minted}{python}
  # python
  def f(x):
      return x**2
\end{minted}
\begin{minted}{ruby}
  # ruby
  def func
      puts "message"
  end
\end{minted}
\inputminted{python}{python-01.py}
\inputminted{ruby}{ruby.rb}
\begin{minted}{python}
  # python
  def f(x):
      return x**2
\end{minted}
\begin{minted}{ruby}
  # ruby
  def func
      puts "message"
  end
\end{minted}
\end{document}
draft=false draft=true
image image
gpoore commented 10 months ago

The development version on GitHub now has a new package option inputlanglinenos that extends the existing langlinenos to cover \inputminted as well. A new release including this package option will be on CTAN soon.