4

I have an equation of the form: constant = f(parm, parm, ...) with a very long list of parameters, which I would like to automatically wrap (after a comma) at the end of each line such that the second and subsequent lines (of parameters) are aligned under right hand side of the equation. But I don't want to manually insert line breaks and \quads for each line. Is there a way to achieve this?

Here's what I would like it to look like, noting another bug whereby negative numbers at the start of a line (e.g. the second line) are rendered as " - 19...", rather than "-19...". I'd love to fix this bug too :) :

enter image description here

Here's the source that generated the above (including manual line breaks and \quads to produce the desired formatting)

\begin{aligned}
  0 &= P(3,1152921504606846976,120,(0,\allowbreak2^{59},\allowbreak-3^{3}\cdot2^{57},\allowbreak3\cdot2^{59},\allowbreak5^{3}\cdot2^{56},\allowbreak-13\cdot2^{58},\allowbreak0,\allowbreak \\ 
    &\quad -19\cdot2^{58},\allowbreak3^{3}\cdot2^{54},\allowbreak2^{55},\allowbreak0,\allowbreak3\cdot31\cdot2^{53},\allowbreak0,\allowbreak2^{53},\allowbreak-7^{2}\cdot2^{52},\allowbreak-19\cdot2^{54},\allowbreak0,\allowbreak-13\cdot2^{52},\allowbreak0,\allowbreak \\ 
    &\quad -113\cdot2^{49},\allowbreak-3^{3}\cdot2^{48},\allowbreak2^{49},\allowbreak0,\allowbreak-5^{3}\cdot2^{47},\allowbreak-5^{3}\cdot2^{46},\allowbreak2^{47},\allowbreak-3^{3}\cdot2^{45},\allowbreak3\cdot2^{47},\allowbreak0,\allowbreak-13\cdot2^{46},\allowbreak \\ 
    &\quad 0,\allowbreak-19\cdot2^{46},\allowbreak3^{3}\cdot2^{42},\allowbreak2^{43},\allowbreak5^{3}\cdot2^{41},\allowbreak3\cdot31\cdot2^{41},\allowbreak0,\allowbreak2^{41},\allowbreak3^{3}\cdot2^{39},\allowbreak-3^{3}\cdot2^{39},\allowbreak0,\allowbreak-13\cdot2^{40},\allowbreak \\ 
    &\quad 0,\allowbreak3\cdot2^{39},\allowbreak7^{2}\cdot2^{37},\allowbreak2^{37},\allowbreak0,\allowbreak-5^{3}\cdot2^{35},\allowbreak0,\allowbreak2^{35},\allowbreak-3^{3}\cdot2^{33},\allowbreak3\cdot2^{35},\allowbreak0,\allowbreak-13\cdot2^{34},\allowbreak-5^{3}\cdot2^{31},\allowbreak \\ 
    &\quad -19\cdot2^{34},\allowbreak3^{3}\cdot2^{30},\allowbreak2^{31},\allowbreak0,\allowbreak-2^{34},\allowbreak0,\allowbreak2^{29},\allowbreak3^{3}\cdot2^{27},\allowbreak-19\cdot2^{30},\allowbreak-5^{3}\cdot2^{26},\allowbreak-13\cdot2^{28},\allowbreak0,\allowbreak \\ 
    &\quad 3\cdot2^{27},\allowbreak-3^{3}\cdot2^{24},\allowbreak2^{25},\allowbreak0,\allowbreak-5^{3}\cdot2^{23},\allowbreak0,\allowbreak2^{23},\allowbreak7^{2}\cdot2^{22},\allowbreak3\cdot2^{23},\allowbreak0,\allowbreak-13\cdot2^{22},\allowbreak0,\allowbreak-3^{3}\cdot2^{19},\allowbreak \\ 
    &\quad 3^{3}\cdot2^{18},\allowbreak2^{19},\allowbreak0,\allowbreak3\cdot31\cdot2^{17},\allowbreak5^{3}\cdot2^{16},\allowbreak2^{17},\allowbreak3^{3}\cdot2^{15},\allowbreak-19\cdot2^{18},\allowbreak0,\allowbreak-13\cdot2^{16},\allowbreak0,\allowbreak3\cdot2^{15},\allowbreak \\ 
    &\quad -3^{3}\cdot2^{12},\allowbreak2^{13},\allowbreak-5^{3}\cdot2^{11},\allowbreak-5^{3}\cdot2^{11},\allowbreak0,\allowbreak2^{11},\allowbreak-3^{3}\cdot2^{9},\allowbreak-113\cdot2^{9},\allowbreak0,\allowbreak-13\cdot2^{10},\allowbreak0,\allowbreak \\ 
    &\quad -19\cdot2^{10},\allowbreak-7^{2}\cdot2^{7},\allowbreak2^{7},\allowbreak0,\allowbreak3\cdot31\cdot2^{5},\allowbreak0,\allowbreak2^{5},\allowbreak3^{3}\cdot2^{3},\allowbreak-19\cdot2^{6},\allowbreak0,\allowbreak-13\cdot2^{4},\allowbreak5^{3}\cdot2,\allowbreak3\cdot2^{3},\allowbreak \\ 
    &\quad -3^{3},\allowbreak2,\allowbreak0,\allowbreak0))
\end{aligned}
3
  • Welcome to TeX.SE.
    – Mico
    Feb 22 at 4:30
  • Regarding the second "bug" you've noticed: it's not a bug but, literally [!], a feature: since there's a \quad instruction before the - (minus) symbols in question, TeX interprets the - symbol as a binary rather than as a unary operator; hence the added space. If you wanted to persist with the align/manual line breaking approach, you'd have to change all instances of - that follow \quad to {-}; this changes the math status of - to "math-ordinary" instead of "math-bin", making TeX interpret the symbol as unary operator, as desired.
    – Mico
    Feb 22 at 7:30
  • Welcome! Excellent question... but if I can point (jokingly) a thing: and now, ask the reader the value of argument 89 😜. Hmmm... I suggest rethinking the notation... maybe an array?
    – Rmano
    Feb 22 at 10:19

2 Answers 2

2

I don't want to manually insert line breaks and \quads for each line

You could place the entire equation in a \parbox with (a) automatic hanging indentation and (b) suppression of full justfication. This allows deleting all instances of \\ &\quad from your code -- they're no longer needed.

I've set the width of the \parbox to 0.85\textwidth to generate the following screenshot; feel free to modify this choice.

enter image description here

\documentclass{article} 
\begin{document}

\begin{equation}
\parbox[b]{0.85\textwidth}{\raggedright\hangafter=1\hangindent=2em$\displaystyle
%% I've deleted all `\\ &\quad` directives
0 = P(3,1152921504606846976,120,(0,\allowbreak2^{59},\allowbreak-3^{3}\cdot2^{57},\allowbreak3\cdot2^{59},\allowbreak5^{3}\cdot2^{56},\allowbreak-13\cdot2^{58},\allowbreak0,\allowbreak-19\cdot2^{58},\allowbreak3^{3}\cdot2^{54},\allowbreak2^{55},\allowbreak0,\allowbreak3\cdot31\cdot2^{53},\allowbreak0,\allowbreak2^{53},\allowbreak-7^{2}\cdot2^{52},\allowbreak-19\cdot2^{54},\allowbreak0,\allowbreak-13\cdot2^{52},\allowbreak0,\allowbreak-113\cdot2^{49},\allowbreak-3^{3}\cdot2^{48},\allowbreak2^{49},\allowbreak0,\allowbreak-5^{3}\cdot2^{47},\allowbreak-5^{3}\cdot2^{46},\allowbreak2^{47},\allowbreak-3^{3}\cdot2^{45},\allowbreak3\cdot2^{47},\allowbreak0,\allowbreak-13\cdot2^{46},\allowbreak0,\allowbreak-19\cdot2^{46},\allowbreak3^{3}\cdot2^{42},\allowbreak2^{43},\allowbreak5^{3}\cdot2^{41},\allowbreak3\cdot31\cdot2^{41},\allowbreak0,\allowbreak2^{41},\allowbreak3^{3}\cdot2^{39},\allowbreak-3^{3}\cdot2^{39},\allowbreak0,\allowbreak-13\cdot2^{40},\allowbreak0,\allowbreak3\cdot2^{39},\allowbreak7^{2}\cdot2^{37},\allowbreak2^{37},\allowbreak0,\allowbreak-5^{3}\cdot2^{35},\allowbreak0,\allowbreak2^{35},\allowbreak-3^{3}\cdot2^{33},\allowbreak3\cdot2^{35},\allowbreak0,\allowbreak-13\cdot2^{34},\allowbreak-5^{3}\cdot2^{31},\allowbreak-19\cdot2^{34},\allowbreak3^{3}\cdot2^{30},\allowbreak2^{31},\allowbreak0,\allowbreak-2^{34},\allowbreak0,\allowbreak2^{29},\allowbreak3^{3}\cdot2^{27},\allowbreak-19\cdot2^{30},\allowbreak-5^{3}\cdot2^{26},\allowbreak-13\cdot2^{28},\allowbreak0,\allowbreak3\cdot2^{27},\allowbreak-3^{3}\cdot2^{24},\allowbreak2^{25},\allowbreak0,\allowbreak-5^{3}\cdot2^{23},\allowbreak0,\allowbreak2^{23},\allowbreak7^{2}\cdot2^{22},\allowbreak3\cdot2^{23},\allowbreak0,\allowbreak-13\cdot2^{22},\allowbreak0,\allowbreak-3^{3}\cdot2^{19},\allowbreak3^{3}\cdot2^{18},\allowbreak2^{19},\allowbreak0,\allowbreak3\cdot31\cdot2^{17},\allowbreak5^{3}\cdot2^{16},\allowbreak2^{17},\allowbreak3^{3}\cdot2^{15},\allowbreak-19\cdot2^{18},\allowbreak0,\allowbreak-13\cdot2^{16},\allowbreak0,\allowbreak3\cdot2^{15},\allowbreak-3^{3}\cdot2^{12},\allowbreak2^{13},\allowbreak-5^{3}\cdot2^{11},\allowbreak-5^{3}\cdot2^{11},\allowbreak0,\allowbreak2^{11},\allowbreak-3^{3}\cdot2^{9},\allowbreak-113\cdot2^{9},\allowbreak0,\allowbreak-13\cdot2^{10},\allowbreak0,\allowbreak-19\cdot2^{10},\allowbreak-7^{2}\cdot2^{7},\allowbreak2^{7},\allowbreak0,\allowbreak3\cdot31\cdot2^{5},\allowbreak0,\allowbreak2^{5},\allowbreak3^{3}\cdot2^{3},\allowbreak-19\cdot2^{6},\allowbreak0,\allowbreak-13\cdot2^{4},\allowbreak5^{3}\cdot2,\allowbreak3\cdot2^{3},\allowbreak-3^{3},\allowbreak2,\allowbreak0,\allowbreak0))
$}
\end{equation}

\end{document}

Addendum: If you can use LuaLaTeX to compile your document, you could employ Lua's string.gsub function to insert all 122 [!] \allowbreak instructions "on the fly", thereby greatly declogging the input code.

% !TEX TS-program = lualatex
\documentclass{article} 
\usepackage{luacode} % for '\luastringN' macro ("unexpanded string-ification")
\begin{document}

\begin{equation}
\parbox[b]{0.85\textwidth}{\raggedright\hangafter=1\hangindent=2em$\displaystyle
0 = \directlua { 
    s=\luastringN{P(3,1152921504606846976,120,(0,2^{59},-3^{3}\cdot2^{57},3\cdot2^{59},5^{3}\cdot2^{56},-13\cdot2^{58},0,-19\cdot2^{58},3^{3}\cdot2^{54},2^{55},0,3\cdot31\cdot2^{53},0,2^{53},-7^{2}\cdot2^{52},-19\cdot2^{54},0,-13\cdot2^{52},0,-113\cdot2^{49},-3^{3}\cdot2^{48},2^{49},0,-5^{3}\cdot2^{47},-5^{3}\cdot2^{46},2^{47},-3^{3}\cdot2^{45},3\cdot2^{47},0,-13\cdot2^{46},0,-19\cdot2^{46},3^{3}\cdot2^{42},2^{43},5^{3}\cdot2^{41},3\cdot31\cdot2^{41},0,2^{41},3^{3}\cdot2^{39},-3^{3}\cdot2^{39},0,-13\cdot2^{40},0,3\cdot2^{39},7^{2}\cdot2^{37},2^{37},0,-5^{3}\cdot2^{35},0,2^{35},-3^{3}\cdot2^{33},3\cdot2^{35},0,-13\cdot2^{34},-5^{3}\cdot2^{31},-19\cdot2^{34},3^{3}\cdot2^{30},2^{31},0,-2^{34},0,2^{29},3^{3}\cdot2^{27},-19\cdot2^{30},-5^{3}\cdot2^{26},-13\cdot2^{28},0,3\cdot2^{27},-3^{3}\cdot2^{24},2^{25},0,-5^{3}\cdot2^{23},0,2^{23},7^{2}\cdot2^{22},3\cdot2^{23},0,-13\cdot2^{22},0,-3^{3}\cdot2^{19},3^{3}\cdot2^{18},2^{19},0,3\cdot31\cdot2^{17},5^{3}\cdot2^{16},2^{17},3^{3}\cdot2^{15},-19\cdot2^{18},0,-13\cdot2^{16},0,3\cdot2^{15},-3^{3}\cdot2^{12},2^{13},-5^{3}\cdot2^{11},-5^{3}\cdot2^{11},0,2^{11},-3^{3}\cdot2^{9},-113\cdot2^{9},0,-13\cdot2^{10},0,-19\cdot2^{10},-7^{2}\cdot2^{7},2^{7},0,3\cdot31\cdot2^{5},0,2^{5},3^{3}\cdot2^{3},-19\cdot2^{6},0,-13\cdot2^{4},5^{3}\cdot2,3\cdot2^{3},-3^{3},2,0,0))}
    tex.sprint ( ( string.gsub ( s , ',' , ',\\allowbreak' ) ) )
    }
$}
\end{equation}

\end{document} 
0
2

Tough situations require strong hammers.

\documentclass{article}
\usepackage{amsmath}
\usepackage{lipsum}

\begin{document}

\lipsum[1][1-4]
\[
% some local settings
\binoppenalty=10000 % no line break after \cdot
\mathchardef\comma=\mathcode`, % save the mathcode
\mathcode`,="8000 % make the comma math active
\thinmuskip=0mu
\begingroup\lccode`~=`,
  \lowercase{\endgroup\def~}{%
    \comma\allowbreak\hspace{3pt plus 2pt}%
}% define it to be a comma with possible line break followed by a flexible space
% now the formula; first a phantom for the alignment
\hphantom{0=P\bigl(}
\parbox{0.8\displaywidth}{\linespread{1.1}\selectfont % less compact typesetting
  \makebox[0pt][r]{$0=P\bigl($}$
  3, 1152921504606846976,120,(0, 2^{59}, -3^{3}\cdot2^{57}, 3\cdot2^{59},
  5^{3}\cdot2^{56}, -13\cdot2^{58}, 0, -19\cdot2^{58}, 3^{3}\cdot2^{54},
  2^{55}, 0, 3\cdot31\cdot2^{53}, 0, 2^{53}, -7^{2}\cdot2^{52},
  -19\cdot2^{54}, 0, -13\cdot2^{52}, 0, -113\cdot2^{49}, -3^{3}\cdot2^{48},
  2^{49}, 0, -5^{3}\cdot2^{47}, -5^{3}\cdot2^{46}, 2^{47}, -3^{3}\cdot2^{45},
  3\cdot2^{47}, 0, -13\cdot2^{46}, 0, -19\cdot2^{46}, 3^{3}\cdot2^{42},
  2^{43}, 5^{3}\cdot2^{41}, 3\cdot31\cdot2^{41}, 0, 2^{41}, 3^{3}\cdot2^{39},
  -3^{3}\cdot2^{39}, 0, -13\cdot2^{40}, 0, 3\cdot2^{39}, 7^{2}\cdot2^{37},
  2^{37}, 0, -5^{3}\cdot2^{35}, 0, 2^{35}, -3^{3}\cdot2^{33}, 3\cdot2^{35},
  0, -13\cdot2^{34}, -5^{3}\cdot2^{31}, -19\cdot2^{34}, 3^{3}\cdot2^{30},
  2^{31}, 0, -2^{34}, 0, 2^{29}, 3^{3}\cdot2^{27}, -19\cdot2^{30},
  -5^{3}\cdot2^{26}, -13\cdot2^{28}, 0, 3\cdot2^{27}, -3^{3}\cdot2^{24},
  2^{25}, 0, -5^{3}\cdot2^{23}, 0, 2^{23}, 7^{2}\cdot2^{22}, 3\cdot2^{23},
  0, -13\cdot2^{22}, 0, -3^{3}\cdot2^{19}, 3^{3}\cdot2^{18}, 2^{19}, 0,
  3\cdot31\cdot2^{17}, 5^{3}\cdot2^{16}, 2^{17}, 3^{3}\cdot2^{15},
  -19\cdot2^{18}, 0, -13\cdot2^{16}, 0, 3\cdot2^{15}, -3^{3}\cdot2^{12},
  2^{13}, -5^{3}\cdot2^{11}, -5^{3}\cdot2^{11}, 0, 2^{11}, -3^{3}\cdot2^{9},
  -113\cdot2^{9}, 0, -13\cdot2^{10}, 0, -19\cdot2^{10}, -7^{2}\cdot2^{7},
  2^{7}, 0, 3\cdot31\cdot2^{5}, 0, 2^{5}, 3^{3}\cdot2^{3}, -19\cdot2^{6}, 0,
  -13\cdot2^{4}, 5^{3}\cdot2, 3\cdot2^{3}, -3^{3}, 2, 0, 0
  )\bigr)$
}
\]
\lipsum[2][1-4]

\end{document}

enter image description here

1
  • 1
    @Mico Right on both accounts! Fixed.
    – egreg
    Feb 22 at 13:16

You must log in to answer this question.

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