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

`newmintedfile` env* to specify additional options #378

Closed goyalyashpal closed 7 months ago

goyalyashpal commented 8 months ago

newmintedfile env to specify additional options (similar to for newminteds env)

useful for options which are fundamentally custom to each use - like firstline etc.

muzimuzhi commented 8 months ago

Not sure I get your point.

\inputminted[<options>]{<lang>}{<filename>} accepts <options> as its first opt-arg, and there's \newmintedfile that can create custom versions of \inputminted, just like \newminted can create custom minted env.

goyalyashpal commented 8 months ago

hi @muzimuzhi the point is simple

goyalyashpal commented 8 months ago

ohhhhww sorry, i meant starred version of newmintedfile env

https://github.com/gpoore/minted/blob/13e25d05cd382cf55f66636e0b1a91d94c2b4e4c/source/minted.dtx#L1550-L1555

goyalyashpal commented 8 months ago

\assign @me

I'd like to work on this.

muzimuzhi commented 8 months ago

So you want \inputminted to accept an optional star which applies showspaces option and a revised \newmintedfile to define starred variant command (not environment) in addition?

Try this:

\begin{filecontents}[noheader]{test.py}
def f(x):
  return x * x + 1
\end{filecontents}
\documentclass{article}
\usepackage{minted}

\makeatletter
\RenewDocumentCommand{\inputminted}{ s O{} m m }{%
  \IfBooleanTF{#1}
    {\inputminted@i{showspaces,#2}{#3}{#4}}
    {\inputminted@i{#2}{#3}{#4}}%
}

\ifthenelse{\boolean{minted@draft}}
  {\newcommand{\inputminted@i}[3]{%
    \begingroup
    \minted@configlang{#2}%
    \setkeys{minted@opt@cmd}{#1}%
    \minted@fvset
    \minted@inputlanglinenoson
    \VerbatimInput{#3}%
    \minted@inputlanglinenosoff
    \endgroup}}%
  {\newcommand{\inputminted@i}[3]{%
    \begingroup
    \minted@configlang{#2}%
    \setkeys{minted@opt@cmd}{#1}%
    \minted@fvset
    \minted@inputlanglinenoson
    \minted@pygmentize[#3]{#2}%
    \minted@inputlanglinenosoff
    \endgroup}}

% #1 = macro name, #2 = language, #3 = options
\renewcommand{\newmintedfile}[3][]{%
  \ifthenelse{\equal{#1}{}}
    {\def\minted@shortname{#2file}}
    {\def\minted@shortname{#1}}%
  % ##1 = star, ##2 = options, ##3 = filename
  \ExpandArgs{c}\NewDocumentCommand{\minted@shortname}{ s O{} m }{%
    \IfBooleanTF{##1}
      {\inputminted@i{#3,showspaces,##2}{#2}{##3}}
      {\inputminted@i{#3,##2}{#2}{##3}}}}
\makeatother

% defines \pythonfile*[<options>]{<filename>}
\newmintedfile{python}{linenos}

\begin{document}
\inputminted{python}{test.py}
\inputminted*{python}{test.py}

\pythonfile{test.py}
\pythonfile*{test.py}
\end{document}

image

goyalyashpal commented 8 months ago

you want \inputminted to accept an optional star

no, only the newmintedfile

\begin{filecontents}[noheader]{test.py}
def f(x):
  return x * x + 1
\end{filecontents}

wow filecontents, thats so cool

muzimuzhi commented 8 months ago

you want \inputminted to accept an optional star

no, only the newmintedfile

Then that seems inconsistent to me: \inputminted doesn't have a starred version, but the custom versions of \inputminted created by \newmintedfile have.

goyalyashpal commented 8 months ago

i would have shared an illustrative working example, but i couldnt find my explorations with defining starred commands.

i will come up with something in few days.

goyalyashpal commented 7 months ago

newmintedfile (pg=29): If no ⟨macro name⟩ is given, then the macro is called \⟨language⟩file. \newmintedfile[⟨macro name⟩]{⟨language⟩}{⟨options⟩}

inputminted (pg=8): Its syntax is \inputminted[⟨options⟩]{⟨language⟩}{⟨filename⟩}

https://github.com/gpoore/minted/blob/13e25d05cd382cf55f66636e0b1a91d94c2b4e4c/source/minted.sty#L1180-L1185

  \expandafter\newcommand\csname\minted@shortname\endcsname[2][]{
    \inputminted[#3,##1]{#2}{##2}}}

as the \newcommand above gives ##1 to the \inputminted's options [ ], it works exactly as it should. following the starred environments by \newminted i shared above, i was trying to pass those in curly braces, which was resulting in the issues.

Working sample:

\begin{filecontents}[noheader]{./test.mysql}
mysql-> select * from firstLine;
mysql-> select * from secondLine;
mysql-> \help contents
mysql-> \h contents
\end{filecontents}

% \documentclass[12pt,a4paper,oneside,titlepage,final]{report}
\documentclass{article}
\usepackage{minted}

\newmintedfile[inputmintedpsql]{psql}{
    % bgcolor=lightgray,
    style=bw,
    keywordcase=upper,
    linenos=true,
}

\begin{document}

\inputmintedpsql[firstline=2, lastline=2]{test.mysql}
% \inputmintedpsql*{firstline=2, lastline=2}{test.mysql} % <-- I was trying to do this due to confusion

\inputmintedpsql[firstline=3, lastline=4, keywordcase=lower]{test.mysql}

\end{document}