How can I use a command inside a minted one-liner?

This code

\mint{console}|sudo groupadd \HADOOPUSER|

currently produces

sudo groupadd \HADOOPUSER

whereas the output I want is

sudo groupadd hduser

  • 1
    What you want to do is not possible. The only way of invoking a command inside minted is via an escape to LaTeX, which is only possible inside comments in the language used. listings can do it, though. – jub0bs May 15 '14 at 14:56
  • @Jubobs Can you please explain to me the first part, with an escape to LaTex, or maybe point me to an example? – Interfector May 15 '14 at 15:05
  • @Jubobs: Never say never. See my answer. – Aditya May 15 '14 at 15:54
  • 1
    @Aditya My understanding is that the OP wants to use minted specifically, though. – jub0bs May 15 '14 at 15:55
  • 1
    @Aditya If you say so. I'm don't know ConTeXt well enough to be able to tell :) – jub0bs May 15 '14 at 16:07

Although I really enjoy minted, I don't think it is the right tool for what you want to do. Invoking macros inside listings typeset by minted is currently very limited (as of v1.7). The package does provide an option called mathescape for allowing escape to LaTeX (i.e. interpret a sequence of token as LaTeX code instead of verbatim), but

  • such an escape to LaTeX is only possible inside comments in the language being used,
  • that feature is mainly oriented towards content of mathematical nature.

Here is an example adapted from the minted manual:

enter image description here



# Returns $\sum_{i=1}ˆ{n}i$
def sum_from_one_to(n):
    r = range(1, n + 1)
return sum(r)
# An escape to LateX won't work outside comments

Clearly, your shell script is not a comment, so there is no way of getting your macro to expand to its replacement text in your listing. Actually, what I gather from the Python source of the lexer you use, console (a.k.a. BashSessionLexer), is that it doesn't define shell to-end-of-line comments (started by a #). Therefore, you can't even escape to LaTeX anywhere in your code (either inside or outside comments), if you use that lexer, as evidenced by the example below.

enter image description here




sudo groupadd #$\HADOOPUSER$

An alternative approach would be to use the listings package instead of minted. With some patch, escaping to LaTeX can be allowed inside listings inline code. If you think that would help, I can expand my answer to provide a solution based on listings.

However. I think it's silly to escape to LaTeX inside inline code. IMO, inline code should be reserved for typesetting a single identifier (keyword, variable, etc.); anything longer than that has the potential to mess up your paragraph. For longer pieces of code (even for a command with arguments such as sudo groupadd hduser), you should use a displayed listing instead:

sudo groupadd hduser

If you follow this rule, you shouldn't need to ever escape to LaTeX inside inline code.

  • If hduser is used in multiple places, it does make sense to have a replacement variable. – Interfector May 15 '14 at 20:41
  • @Interfector I agree; code duplication is evil. However, what you want to do is currently not possible with minted. You might have better luck with lstlisting environments (from the listings package). – jub0bs May 15 '14 at 20:47

I am providing a ConTeXt solution, because I do not know about the state of LuaTeX packages in LaTeX.

One possibility is to use Lua (rather than TeX) to translate your input. For that, ConTeXt provides a m-translate module which can be used with the vim module. (The vim module uses vim to generate syntax highlighting; so, in principal, this should work with minted+LaTeX as well).



\definevimtyping[console][syntax=bash, before=\enableinputtranslation, after=\disableinputtranslation]


Translated: \inlineconsole{sudo groupadd HADOOPUSER}

This only works inside console environment, not regular text. For example


which gives

enter image description here

This works as follows: The text sudo groupadd HADOOPUSER is written to an external file and translated to \SYNBOL{}sudo groupadd HADOOPUSER\SYNEOL{}. When the file is read by TeX, \enableinputtranslation is active, so the line is converted to \SYNBOL{}sudo groupadd hduser\SYNEOL{} and passed on to TeX, where the macros of t-vim add the necessary syntax highlighting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.