0

Is there a way to mint a line of code inline, where the code is read from a file?

\mintinline can only typeset code that is passed directly to the command, in between two delimiters (e.g. \mintinline{latex}{\LaTeX}), and \inputminted typesets the code in its own box separate from the paragraph.

I would like a mix of the two, something like (the hypothetical) \inputmintinline. The minted manual mentions nothing of the sort.


I would use this because I want to make a command that reads writes its argument verbatim to a file, typesets the content as minted but then also interprets the content as LaTeX, expanding/executing any macros in it, typesetting the result. Related to this answer.

I have already got it figured out for "out-of-line"code, but not for inline code since there is no \inputmintinline command.

Anakhand
  • 436

1 Answers1

3

Not sure what's the usefulness of this:

\begin{filecontents*}{\jobname-test.tex}
\LaTeX
\end{filecontents*}

\documentclass{article}
\usepackage{minted,catchfile}

\newcommand\inputmintinline[3][]{%
  \begingroup\everyeof{}%
  \CatchFileDef\temp{#3}{}%
  \scantokens\expandafter{%
    \expandafter\processinputmintinline\expandafter{\temp}{#1}{#2}%
  }\unskip
  \endgroup
}
\newcommand{\processinputmintinline}[3]{\mintinline[#2]{#3}{#1}}

\begin{document}

X\inputmintinline{latex}{\jobname-test.tex}X

X\mintinline{latex}{\LaTeX}X

\end{document}

enter image description here

With expl3:

\begin{filecontents*}{\jobname-test.tex}
\LaTeX
\end{filecontents*}

\documentclass{article}
\usepackage{minted,xparse,l3cctab}

\ExplSyntaxOn
\NewDocumentCommand{\inputmintinline}{O{}mm}
 {
  \group_begin:
  \cctab_begin:N \c_other_cctab
  \char_set_catcode_ignore:n { `\^^M } % ignore the endline
  \file_get:nnN { #3 } { } \l_tmpa_tl
  \cctab_end:
  \anakhand_mintintline:nnV { #1 } { #2 } \l_tmpa_tl
  \group_end:
}
\cs_new_protected:Nn \anakhand_mintintline:nnn
 {
  \mintinline[#1]{#2}{#3}
 }
\cs_generate_variant:Nn \anakhand_mintintline:nnn { nnV }
\ExplSyntaxOff

\begin{document}

X\inputmintinline{latex}{\jobname-test.tex}X

X\mintinline{latex}{\LaTeX}X

\end{document}
egreg
  • 1,121,712