I need to write some equations that contain something like (A | B).

I could write \left( A \middle| \right), but this changes spacing around external parentheses, as expained in many topics like this.

There is a nice working solution with \DeclarePairedLimiterX as explained in this answer. However, for many reasons I would like to use \middle| and fix the spacing around it (so that it is the same as around \mid), in the same way as \left and \right are fixed in this answer or by the package mleftright (see this answer). In other words, I would like | to stretch like \middle| and to have the same spacing as \mid. Is it possible? I'm using mathtools.

Thank you!

  • Honestly, I've always just used the lazy solution: \,\middle|\,. – David Richerby May 16 '18 at 8:57

An engine-independent solution (albeit one I still have a murky feeling about...)




(A \mid B)
\left(A \middle| B\right)
\left(\frac{A}{B} \middle\Vert C_{(A \mid B) \left( A \middle| B \right)} \right)


enter image description here

  • +1. And it works with LuaLaTeX too, not just pdfLaTeX... – Mico May 16 '18 at 7:37
  • 1
    @Mico Well, yes, I meant that Henri's solution depends on lua code while this one doesn't. I'll rephrase. – campa May 16 '18 at 7:39
  • Thank you, very simple and also similar to the fixes I used for left and right – Taekwondavide May 16 '18 at 13:23
  • 1
    @Taekwondavide Maybe instead of redefining \middle (I'm always cautious about redefining TeX primitives) you might want to define something like \mmiddle (like \mleft/\mright). – campa May 16 '18 at 15:26
  • Thank you. I'm considering that, I'm still in doubt about using redefined \middle and \originalmiddle or \mmiddle and original \middle. I guess I have to check whether any package is using \middle or not – Taekwondavide May 16 '18 at 16:05

You can use some Lua magic.

The three snippets below are one file. They are only split up because Stack Exchange does not support mixed highlighting.


local noad_id = node.id("noad")
local fence_id = node.id("fence")
local inner_subtype = 9 -- see texnodes.w
local middle_subtype = 2 -- see texnodes.w

local function is_vert(delim)
    return delim.small_fam == 2
        and delim.small_char == 106
        and delim.large_fam == 3
        and delim.large_char == 12

local kern = node.new("kern",99) -- 99 = math kern
kern.kern = 5 * 2^16 -- 5 pt (TODO: load this value from the font)

local function adjust_mid_spacing(head)
    for n in node.traverse(head) do
        if n and n.nucleus and n.nucleus.head then
        elseif n.id == fence_id and n.subtype == middle_subtype then
            if is_vert(n.delim) then
    return head

                           function(head, display_type, need_penalties)
                               head = adjust_mid_spacing(head)
                               return node.mlist_to_hlist(head, display_type, need_penalties)
                           "adjust spacing around mid")  


$\left( A \middle| B \right)_{\left( A \middle| B \right)_{\left( A \middle| B \right)}}$

$(A \mid B)_{(A \mid B)_{(A \mid B)}}$


enter image description here

  • I think it's simpler in luatex (Umiddle class 2) – David Carlisle May 16 '18 at 8:18

luatex allows the mathclass to be specified on delimiters and you want 2 (mathbin) here.

enter image description here



$\left( A \middle| B \right)$

$\Uleft( A \Umiddle class 2 | B \Uright)$

  • the spacing looks off to me and if you omit the B you get a "this can't happen" error and no output, but I expect those issues will get fixed, luatex is under very active development. – David Carlisle May 16 '18 at 8:41
  • Cool. Wouldn't mathrel be more appropriate here though? I think \mid has class 3. – Circumscribe May 16 '18 at 8:43
  • @Circumscribe possibly (I think spacing is a bit wrong for that as well so it's not too important at the moment which you pick:-) – David Carlisle May 16 '18 at 8:46
  • Thank you! I don't know luatex, is it 100% compatible with latex and texlive/miktex? – Taekwondavide May 16 '18 at 15:59
  • 1
    luatex is included in both texlive and miktex as an alternative to pdftex, it is not 100% compatible. Like xetex it uses (mostly) Unicode fonts installed in the system rather than specific tex-encoded fonts, however most latex documents can be used with xelatex or lualatex with small changes, we have answers on this site giving the differences @Taekwondavide – David Carlisle May 16 '18 at 16:02

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.