TransWikia.com

Why can't I use @MakeShortVerb with minted?

TeX - LaTeX Asked on November 29, 2021

shortvrb‘s MakeShortVerb allows defining single active tokens, e.g. |, in a way that makes |foo| equivalent to verb|foo|. To do this, | is defined to expand to verb| and added to both dospecials and @sanitize. Internally, MakeShortVerb| expands to def@shortvrbdef{verb}@MakeShortVerb| and @shortvrbdef is later used to set the expansion of |.

I want to use the same mechanism but with minted. In order to use the minted macro code (as defined in the MWE below) insead of verb, I defined

defMakeShortCode{def@shortvrbdef{code}@MakeShortVerb}

However, I get the error

! Paragraph ended before minted@inline@ii was complete.

when using the shorthand. If I manually define

def|{code|}

the shorthand works fine. Inspection of the definition of | in both cases shows that they are indeed identical, so the only difference should be the registration of | in dospecials and @sanitize.

Why exactly does this error occur? How can I get around this without braking e.g. the verbatim environment? Is this a bug in minted or is there a good reason for this behavior?


documentclass{article}

usepackage{xparse}
usepackage{minted}
usepackage{shortvrb}

newmintinline[code]{latex}{}

makeatletter
  defMakeShortCode{def@shortvrbdef{code}@MakeShortVerb}
  defMakeShortLstinline{def@shortvrbdef{lstinline}@MakeShortVerb}
makeatother

ExplSyntaxOn
  NewDocumentCommanddecompose{ m }
    {
      {
        ttfamily
        tl_analysis_map_inline:Nn #1 
          {
            exp_last_unbraced:No token_to_str:N { ##1 }
            ensuremath { sb {##3} }
          }
      }
    }
ExplSyntaxOff

begin{document}

catcode`|=active

def|{code|}
|foo|

decompose|

MakeShortCode|
%|foo| % This will cause an error.

decompose|

end{document}

2 Answers

Not sure why trying to overcomplicate things:

documentclass{article}

usepackage{minted}

newmintinline[code]{latex}{}

catcode`|=active
makeatletter
protecteddef|{%
  ifmmode
    expandafter@firstoftwo
  else
    expandafter@secondoftwo
  fi
  {string|}{code|}%
}
makeatother

begin{document}

|foo|

$|a+b|$

end{document}

enter image description here

A more complicated version that allows to locally remove a shorthand.

documentclass{article}

usepackage{minted}

newmintinline[code]{latex}{}

makeatletter
newcommand{makemintedshorthand}[1]{%
  expandafterchardefcsname catcode#1endcsname=catcode`#1
  begingrouplccode`~=`#1lowercase{endgroup
    protecteddef~{%
      ifmmode
        expandafter@secondoftwo
      else
        expandafter@firstoftwo
      fi
      {code~}{#1}%
  }}%
  catcode`#1=active
}
newcommand{removemintedshorthand}[1]{%
  ifcsname catcodestring#1endcsname
    catcode`#1=csname catcodestring#1endcsname
  fi
}
makeatother

makemintedshorthand{|}

begin{document}

|foo|

$|a+b|$

begingroup
removemintedshorthand{|}
code$abc|def$ code|abc$def|

verb|xyz| verb"a|b"
endgroup

|foo|

end{document}

enter image description here

Answered by egreg on November 29, 2021

The problem is that when code|foo| is executed, TeX only scans for the second | after dospecials changed its category code to 12. @MakeShortVerb defines |13 to expand to code0|13 (the indices denote category codes), so |foo| effectively expands to code0|13foo0|12, which can't work.

We can fix this by modifying MakeShortCode to define |13 as code0|12.

documentclass{article}

usepackage{etoolbox}
usepackage{minted}
usepackage{shortvrb}

newmintinline[code]{latex}{}

makeatletter
  defMakeShortCode{def@shortvrbdef{code}@MakeShortCode}
  let@MakeShortCode@MakeShortVerb
  patchcmd@MakeShortCode
    {expandaftergdefexpandafter~expandafter{@shortvrbdef~}}
    {xdef~{expandafternoexpand@shortvrbdefexpandafter@gobblestring#1}}
    {}{}
makeatother

MakeShortCode|

begin{document}makeatletter

|foo{bar} baz|

begin{minted}{latex}
  I wrote |foo{bar} baz|.
end{minted}

end{document}

Answered by schtandard on November 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP