2

As a copy editor, my job involves optimizing the layout of scientific articles written by authors directly in LaTeX to ensure they conform to the styles of the journals I work for.

A common issue I encounter is the inappropriate use of the \left and \right macros, especially when equations are split across multiple lines. This often results in mismatched bracket sizes, which is visually unappealing and incorrect.

For instance, in the following equation (real case):

\begin{align}
P^{ij} & = -2 \sqrt{-g}\left\{G^{ijlm}\dot K_{lm} + \alpha K^{ij} K -\alpha g^{ij} K_{lm} K^{lm}+\alpha~\,R^{\hspace{-0.4cm}3\hspace{0.3cm}ij}\right. \nonumber \\
&\left.\qquad \qquad\quad  -g^{ij}\left[\left(\frac{\alpha}3+2\beta\right)(~R^\3-3K_{lm}K^{lm}+K^2)+\gamma\right]\right\}, \label{Pijsol} 
\end{align}

enter image description here

the corresponding brackets are of different sizes.

To speed up the correction of these cases, I thought I could define macros that allow me to replace \left/\right with, for example, \biggl/\biggr without needing to remove the corresponding \right./\left. (to simplify and expedite my work).

For example:

\begin{align*}
x&=\left(y\right.\\
 &\left.\qquad +\frac{1}{2}\right)
\end{align*}

should become something like:

\begin{align*}
x&=\lbiggl(y\right.\\
 &\left.\qquad +\frac{1}{2}\rbiggl)
\end{align*}

and work like the following code:

\begin{align*}
x&=\biggl(\left.y\right.\\
 &\left.\qquad +\frac{1}{2}\right.\biggr)
\end{align*}

The idea is to write a function in Emacs Lisp that automatically performs the replacements \left --> \lbiggl and \right --> \rbiggr for the first corresponding brackets after the cursor, simply ignoring the \right./\left..

Does my idea make sense? How can I define \lbiggl/\rbiggr and similar commands to achieve the desired effect?

I am also open to solutions that address the problem in other ways. (I am aware that alternative methods, such as \vphantom, can be used, but the solution I am looking for should be focused on speed of implementation rather than formal rigor in the code.)

13
  • 1
    \left and \right are usually good for quick typesetting, but seldom for really good typesetting → tex.stackexchange.com/q/173717/277964. For good typesetting also visual control is needed and therefore simple automatism more and more often fail. However, if you want an automatism, see e.g. Torbjørn's answer to tex.stackexchange.com/q/21290/277964.
    – cabohah
    Commented Jul 2 at 6:42
  • I am not looking for a fully automated solution, but rather a way to visually and quickly correct cases like the one described. My idea is to trigger a function with a keyboard shortcut without having to manually modify the code, allowing for targeted corrections rather than changes applied to the entire document. i just need, if possible, the right definition ot the macro.
    – Gabriele
    Commented Jul 2 at 6:50
  • So I've misunderstood your “The idea is to write a function in Emacs Lisp that automatically performs the replacements”. Sorry. BTW: If you search for an emacs solution, e.g., to find/visualize/replace the corresponding \right to a \left or all \right\left pairs with & resp. \\ in between, maybe an emacs lisp forum could help you.
    – cabohah
    Commented Jul 2 at 6:53
  • 2
    this seems fundamentally a bad idea, why not do a query replace replacing \left. and \right. by nothing and then \left and \right by (say) \Bigl and \Bigr or by a custom lisp loop that asks the size in each case. adding spurious \left.\right. will add unwanted horizontal space Commented Jul 2 at 7:51
  • @DavidCarlisle What you suggest is exactly what I am doing now. Currently, my function asks me whether to replace '\left'/'\right' with '\biggl'/'\biggr' or '\Bigg'l/'\Biggr' and then runs a query-replace "\right." --> "" etc. in the affected region. However, imagine having a 30-line equation and the parentheses you want to resize are on the first and last lines. The query replace will catch all the nested \right./\left within the 30 lines. Why subject myself to such torture?! My goal is to simplify and speed up this type of correction.
    – Gabriele
    Commented Jul 2 at 10:00

2 Answers 2

1

Adopt a better input style, so you can easily add bits.

In the particular case, I believe it's better to use multline rather than align, but it's not the main point.

Balancing \left and \right in input allows to easily spot the appropriate place where inserting a phantom.

I changed some syntactic constructs you use.

  1. The “prescript” to R should definitely not added with negative and positive spaces; the \mathop{}\! part guarantees that a thin space will be added if \iiiR follows an ordinary symbol. Using ~ is not really the best and in the second instance, a gap between the parenthesis and 3R would be ambiguous.

  2. \frac{\alpha}3 should really be \frac{\alpha}{3}`. You gain two keystrokes at the expense of uniformity and clarity.

  3. \dot K must be \dot{K}. Yes, the former syntax works but what about when your text editor takes the initiative and split \dot at the end of a line and K at the start of a new one?

\documentclass{article}
\usepackage{amsmath}

\newcommand{\iiiR}{\mathop{}\!{}^{3\!}R}

\begin{document}

\begin{multline}
P^{ij} =
-2 \sqrt{-g}\left\{ \vphantom{\frac{\alpha}{3}} % <--- for balancing
  G^{ijlm}\dot{K}_{lm} + \alpha K^{ij} K -
  \alpha g^{ij} K_{lm} K^{lm}+\alpha \iiiR^{ij}
\right. \\
\left.
  -g^{ij}\left[
    \left(\frac{\alpha}{3}+2\beta\right)(\iiiR-3K_{lm}K^{lm}+K^2)+\gamma
  \right]
\right\}, \label{Pijsol} 
\end{multline}

\end{document}

enter image description here

5
  • 1
    I thought I was clear. I am not the one writing the LaTeX code; I am only responsible for standardizing it before formatting. If the code generates an acceptable layout, it doesn't make sense for me or my company to spend time making it "perfect" from a syntax or LaTeX best practices perspective. I am interested in a practical and quick solution, even if it's rough and dirty.
    – Gabriele
    Commented Jul 2 at 9:48
  • Obviously, if I had to write a document from scratch, I would do it completely differently. The first example of the equation is a "real" case, written in LaTeX by a scientist whose priority is not to know LaTeX perfectly. In my situation, would you really spend time correcting this kind of details in every single one of the dozens of equations in a paper of 100 or more pages? From my (economic) point of view, it is sufficient that the equation is readable and that it meets the guidelines required by the publisher.
    – Gabriele
    Commented Jul 2 at 9:49
  • @Gabriele Yes, I'd fix the obvious inconsistencies that could lead, as in the present case, to really bad typesetting.
    – egreg
    Commented Jul 2 at 10:30
  • Okay, imagine that your salary depends on how many papers you format in a month and that these details are irrelevant to your publisher... I know the solutions you propose are always impeccable, and you have no idea how much time you've saved me with the answers to my strange questions! I am grateful to you, but I doubt that your approach would be sustainable in a productive context.
    – Gabriele
    Commented Jul 2 at 10:45
  • @Gabriele The main suggestion for this case was to change the input style to have more meaningful line breaks.
    – egreg
    Commented Jul 2 at 12:14
1

You can use the longmath package to create delimiter groups extending over multiple lines or cells.

It allows to link brackets in different (sub)formulas to each other and adapt their sizes to the content between them automatically.

\documentclass{article}
\usepackage{longmath}
\usepackage{amsmath}
\begin{document}
\begin{align*}
   x &= \lleft {label1} ( 1 + 2 + 3 \\
     &  \qquad + 4 + \frac{1}{2} \rright {label1} )
\end{align*}
\end{document}

Here, label1 is a label that links the ( and ) to each other. Alternatively, the two array cells can be linked automatically.

\documentclass{article}
\usepackage{longmath}
\usepackage{amsmath}
\begin{document}
\begin{align*}
   x &= \lleft ( 1 + 2 + 3 \pushdelimiter \\
     &  \pulldelimiter \qquad + 4 + \frac{1}{2} \rright )
\end{align*}
\end{document}

Both examples produce the same output.

enter image description here

3
  • You could improve your answer by adding a screenshot of the result, so everyone can see it and compare it with other answers directly, without first compiling it.
    – dexteritas
    Commented Jul 2 at 9:57
  • Thank you, I didn't know about this package and this solution. It might be useful for other kinds of problems. However, in this case, it's not helpful because I would still have to manually remove '\right.' and '\left.' (if I understood correctly).
    – Gabriele
    Commented Jul 2 at 10:14
  • @Gabriele The invisible delimiters can stay there. In fact, you can attach labels to them to align the groups that belong together. The longmath package also provides macros \pushdelimiter and \pulldelimiter to link one array cell to the next one automatically. Commented Jul 4 at 6:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .