0

Consider the following macro:

\newcommand{\interpretation}[1]{\left[ #1 \right]_{\mathcal{N}}}

This gives the following output on argument a:

example 1

So far, so good.

Now, suppose for example that I pass in a long argument that includes all sorts of things, such as [(\phi \land \psi)]_A \lor [(\neg \chi \rightarrow \zeta)]_C:

example 2

(Note that none of this is meant to make any mathematical sense - just random symbols for an example.)

It can get hard to see which set of brackets matches up with which, etc. What I would like is for the macro to conditionally add whitespace around the outermost brackets if the argument is visually wide. For example, like this:

example 3

If I unconditionally add space, it will do it to all arguments, even our simple a from before:

example 4

which I would like to avoid. Is there any way for the macro to 'render' its argument and add spaces conditionally based on its width? I am open to any packages etc.

alex
  • 1
  • Welcome to TeX.SE! – Mensch Jan 21 '24 at 17:49
  • Can you define what you mean by "visually wide?" – Werner Jan 21 '24 at 17:50
  • I would recommend adding it manually on a case-by-case basis, where you control where and how much to add. Trying to automate will require added checking on your part, to make sure the automation made the same choices as your brain. – Steven B. Segletes Jan 21 '24 at 18:10
  • it's probably more common (and easier) to vary the size of the brackets, making the outer ones larger rather than adding space. – David Carlisle Jan 21 '24 at 18:10
  • @DavidCarlisle I'm sure. I was actually planning to adjust bracket size too, but I wanted to figure this out first. – alex Jan 21 '24 at 18:48
  • @Werner The resulting rendered text's width (not, say, the argument width in characters or something like that). – alex Jan 21 '24 at 18:48

1 Answers1

1

You can adjust the 0.05 factor to taste

enter image description here

\documentclass{article}

\newcommand{\interpretation}[1]{% % avoid automatic left-right https://tex.stackexchange.com/a/173740/1090 \left[% \sbox0{$#1$}% \hspace{.05\wd0}{#1}\hspace{.05\wd0}% \right]_{\mathcal{N}}} \begin{document}

$\interpretation{a}$

$\interpretation{a+b}$

$\interpretation{a+b+c+d}$

$\interpretation{a+b+c+d+xyz}$

\end{document}

David Carlisle
  • 757,742
  • Thanks for this answer. It seems to work quite well. Do you think you can explain a little bit about what is going on inside the macro in case people might want to modify it? – alex Jan 21 '24 at 22:59
  • oh it just sets the argument in a box and adds a space that is 5% of the width of that box. @alex – David Carlisle Jan 21 '24 at 23:01
  • For others who want to learn more about this, I found this: https://latexref.xyz/_005csbox-_0026-_005csavebox.html – alex Jan 22 '24 at 02:46
  • Do you know why the following is having trouble? I am trying to scale the symbol also, but am getting cryptic errors: \scaleobj{0.1 \wd0}{\llbracket}. Is 0.1 \wd0 not a number? – alex Jan 22 '24 at 02:49