Forcing mintinline background to have the height of the line

I'm having trouble with \mintinline, because I am using a black background for it, and the characters render in a way that I find unpleasing. Namely, when doing something like:

\documentclass{article}

\usepackage{minted}
\usemintedstyle{monokai}

\setminted{
bgcolor=black,
}

\begin{document}

Hello, \mintinline{coq}{+}, \mintinline{coq}{*}, \mintinline{coq}{-}, can you have the same height?

\end{document}


I would like each character's background to take the same height, as I find the difference jarring:

IS there a trick I can use to force the height to be fixed?

• What version of the minted package do you use (see log file)? I can't reproduce this behaviour with version 2.5, all boxes have the same height here – siracusa Aug 19 '18 at 3:18
• Fairly sure I'm also on 2.5... Might be relevant: I'm using xetex. – Ptival Aug 19 '18 at 8:53

The documentation of minted states that the bgcolor option puts \mintinline inside a \colorbox. Therefore, as a solution, you can define your own \colorbox with a fixed height and use that instead of the automatic colorbox used by minted.

To set the height of the colorbox, a \parbox can be used within the colorbox (as in, for example, https://tex.stackexchange.com/a/232491/).

MWE:

\documentclass{article}
\usepackage{minted}
\usemintedstyle{monokai}
\begin{document}

\newcommand{\inlinebg}[1]{%
\colorbox{black}{%
\parbox[b][0.6em]{\widthof{\mintinline{coq}{#1}}}{\mintinline{coq}{#1}}%
}%
}

\setminted{bgcolor=black}
Without custom colorbox/parbox:

Hello, \mintinline{coq}{+}, \mintinline{coq}{*}, \mintinline{coq}{-}, can you have the same height?

\setminted{bgcolor={}}
\vspace{1cm}
With custom colorbox/parbox:

Hello, \inlinebg{+}, \inlinebg{*}, \inlinebg{-}, can you have the same height?
\end{document}


Result:

• Great! Had to bump it from 0.6em to ~1em because I have some tall glyphs in there, but it did the trick! – Ptival Aug 22 '18 at 4:23
• I realized that \widthof on \mintinline returns a width that contains some left-and-right margins, making the parbox slightly larger horizontally than the original. Any workaround? – Ptival Sep 25 '18 at 2:16
• You can subtract a small amount (maybe 1pt) from the width, see for example tex.stackexchange.com/questions/149045/… (two answers with different approaches, but both of them should work). – Marijn Sep 25 '18 at 7:18