LaTeX forum ⇒ Math & ScienceHow to set a default alignment in math blocks?

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
eugene8086
Posts: 9
Joined: Thu Apr 02, 2015 3:17 pm

How to set a default alignment in math blocks?

Postby eugene8086 » Sun Apr 12, 2015 6:10 am

There is such a code:
  1. \begin{document}
  2. \begin{preview}
  3. $\frac{(1+mx)^n-(1+nx)^m}{x^2} = \frac{x^2\left( (\dots)x+ \frac{mn(n-m)}{2} \right)}{x^2} = \left( (\dots)x+ \frac{mn(n-m)}{2}\right) =\frac{mn(n-m)}{2}.$
  4. \end{preview}
  5. \end{document}

The result:
Image

As seen, the default alignment is justify. How to set the default alignment in math blocks on the left side?

I know that an alignment usually done by directives align, flalign etc, but I would like to set the default alignment for math-blocks directly, generally.

I'm doing the web generator image formulas for the forum, so it is impossible to know in advance what the expression will be substituted between $ and $. For this reason I can not use align, flalign and other special blocks for alignment.

I tried to do so:
  1. \begin{document}
  2. \begin{preview}
  3. \begin{flushleft}
  4. $\displaystyle <myexpression>$
  5. \end{flushleft}
  6. \end{preview}
  7. \end{document}


If the <myexpression> does not contain newline \\, then this works fine (automatically wraps and left-aligned), but but if the string contains newline, it produces an error:
  1. ! LaTeX Error: There's no line here to end.
  2. See the LaTeX manual or LaTeX Companion for explanation.
  3. Type H <return> for immediate help.
  4. ... l.25 ...shleft}$\displaystyle \sin x+\cos x=1,4 \\
  5. \frac{1 {\sqrt{2}}\sin x+...
  6. ?
  7. ! Emergency stop.

Tags:

User avatar
Johannes_B
Site Moderator
Posts: 4044
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Sun Apr 12, 2015 11:32 am

You are in inline math mode there and just switch the symbols to displaystyle. You need to use a displayed math environment to align stuff.

  1. \documentclass{article}
  2. \usepackage{mathtools}
  3. \usepackage{preview}
  4. \begin{document}
  5. \begin{preview}
  6. \[\frac{(1+mx)^n-(1+nx)^m}{x^2} =
  7. \frac{x^2\left( (\dots)x+ \frac{mn(n-m)}{2} \right)}{x^2}
  8. = \left( (\dots)x+ \frac{mn(n-m)}{2}\right)
  9. =\frac{mn(n-m)}{2}.\]
  10. \end{preview}
  11. \begin{preview}
  12. \begin{align*}
  13. \frac{(1+mx)^n-(1+nx)^m}{x^2} &=
  14. \frac{x^2\left( (\dots)x+ \frac{mn(n-m)}{2} \right)}{x^2} \\
  15. &= \left( (\dots)x+ \frac{mn(n-m)}{2}\right)
  16. =\frac{mn(n-m)}{2}.
  17. \end{align*}
  18. \end{preview}
  19. \end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

User avatar
Stefan Kottwitz
Site Admin
Posts: 9406
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sun Apr 12, 2015 12:26 pm

eugene8086 wrote:As seen, the default alignment is justify. How to set the default alignment in math blocks on the left side?


As you mean in "math text blocks", you could use \raggedright for the text which affects inline math too. For displayed math, you could use the fleqn option (flush left equations). I noticed, you tried the flushleft environment, which is similar to \raggedright.

eugene8086 wrote:I'm doing the web generator image formulas for the forum, so it is impossible to know in advance what the expression will be substituted between $ and $. For this reason I can not use align, flalign and other special blocks for alignment.


Humans are writing the formulas, right? I would allow then to use align etc. so they can write also complex equations. I can imagine, users might complain if they would only be allowed to do inline math.

eugene8086 wrote:If the <myexpression> does not contain newline \\, then this works fine (automatically wraps and left-aligned), but but if the string contains newline, it produces an error:
  1. ! LaTeX Error: There's no line here to end.


\\ would work without flushleft and \raggedright, but actually they don't belong to a math formula if it's not a multi-line formula. \\ in math formulas are for ending lines in blocks such as matrices or equation systems. You could filter them out if they are not within another environment.

Anyway, I would allow align etc. and require that users use correct LaTeX syntax.

Stefan
Site admin

eugene8086
Posts: 9
Joined: Thu Apr 02, 2015 3:17 pm

Postby eugene8086 » Sun Apr 12, 2015 8:14 pm

As you mean in "math text blocks", you could use \raggedright for the text which affects inline math too. For displayed math, you could use the fleqn option (flush left equations). I noticed, you tried the flushleft environment, which is similar to \raggedright.
I already tried to use \RaggedLeft. It gives the same error, if there is line breaks in expression. As I understand, \RaggedLeft is synonym of \begin{flushleft} \end{flushleft}.

Humans are writing the formulas, right? I would allow then to use align etc. so they can write also complex equations. I can imagine, users might complain if they would only be allowed to do inline math.
Probably you have understood me correctly. I need to improve my English :roll:

I allow users to insert special blocks (align, flalign etc). But I check: if the expression does not have special blocks, then I put the expression in the "one-dollar" block.

Anyway, I would allow align etc. and require that users use correct LaTeX syntax.
I think in the web forum is not very convenient. I would like to simplify the use.

Do I understand correctly, that the breaks \\ is not recommended by syntax for use in one-dollar math blocks without(outside) the special blocks (matrix, align, flalign etc)?

I have another problem. If the formula does not fit to the page width, then part of the formula is clipped to the right. So it should be?

UPD.
If I include the package \usepackage[document]{ragged2e}, I get the error, if used break line in the one-dollar block, even if the document does not contain \raggedright directive.

User avatar
Stefan Kottwitz
Site Admin
Posts: 9406
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sun Apr 12, 2015 9:34 pm

eugene8086 wrote:Do I understand correctly, that the breaks \\ is not recommended by syntax for use in one-dollar math blocks without(outside) the special blocks (matrix, align, flalign etc)?


Yes, \\ is for ending a line in multi-line environments, such as those math blocks or tabular environments. You can consider even a flush left set of short lines as multi-line block. In contrast, body text, which flows and is wrapped and justified, should not be wrapped by \\. Here we use paragraph breaks (by an empty line), not line breaks. Especially beginners often use \\, also for generating empty lines as vertical space.

eugene8086 wrote:If the formula does not fit to the page width, then part of the formula is clipped to the right.


Inline formulas can be broken automatically at relation signs (=, <, >, ...) and binary operation symbols (+, -, ....) by design. If they are longer, it's more a sign that they should be displayed, not inline.

TeX's and maths rules are more suited for books and articles, the default rules are perhaps not optimal for web pages with narrow columns.

Stefan
Site admin


Return to “Math & Science”

Who is online

Users browsing this forum: No registered users and 4 guests