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).
Post Reply
eugene8086
Posts: 9
Joined: Thu Apr 02, 2015 3:17 pm

How to set a default alignment in math blocks?

Post by eugene8086 »

There is such a code:

Code: Select all

\begin{document}
\begin{preview}
$\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}.$
\end{preview}
\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:

Code: Select all

\begin{document}
\begin{preview}
\begin{flushleft}
$\displaystyle <myexpression>$
\end{flushleft}
\end{preview}
\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:

Code: Select all

! LaTeX Error: There's no line here to end.
See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...                                                         l.25 ...shleft}$\displaystyle \sin x+\cos x=1,4 \\
                                                   \frac{1 {\sqrt{2}}\sin x+...
?
! Emergency stop.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to set a default alignment in math blocks?

Post by Johannes_B »

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.

Code: Select all

\documentclass{article}
\usepackage{mathtools}
\usepackage{preview}
\begin{document}
\begin{preview}
	\[\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}.\]
\end{preview}
\begin{preview}
	\begin{align*}
		\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}.
	\end{align*}
\end{preview}
\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: 10290
Joined: Mon Mar 10, 2008 9:44 pm

How to set a default alignment in math blocks?

Post by Stefan Kottwitz »

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:

Code: Select all

! 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
LaTeX.org admin
eugene8086
Posts: 9
Joined: Thu Apr 02, 2015 3:17 pm

How to set a default alignment in math blocks?

Post by eugene8086 »

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: 10290
Joined: Mon Mar 10, 2008 9:44 pm

How to set a default alignment in math blocks?

Post by Stefan Kottwitz »

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
LaTeX.org admin
Post Reply