0

In display math (employing $$...$$) content normally gets aligned centered. But, I need it to be left aligned. Any suggestions?

Example

$$f(x) = \frac{{{{\sin }^2}x{\mkern 1mu} \cos x}}{{\sin x + \cos x}} -
         \frac{1}{4}\tan (\frac{\pi }{4} - x){\mkern 1mu} {\mkern 1mu} 
         \forall {\mkern 1mu} x \in R - \{ n\pi  - \frac{\pi }{4}\} ,n \in I$$
Ruben
  • 13,448
Rajesh
  • 1

2 Answers2

2

Is fleqn what you are looking for? (By the way, I rewrote the code in a more clear way, at least to me)

\documentclass[fleqn]{scrartcl}

\usepackage{mathtools,amssymb}
\usepackage{kantlipsum} % Just for this example (the \kant command)

\begin{document}
\kant*[1]
\[
  f(x) = \frac{\sin^2 x \cos x}{\sin x + \cos x}
         - \frac{1}{4} \tan\Bigl(\frac{\pi}{4} - x\Bigr) 
  \quad
  \forall x \in R \setminus \Bigl\{ n\pi - \frac{\pi}{4} \Bigr\}, \ n \in I
\]
\kant*[2]
\end{document}

enter image description here

Manuel
  • 27,118
2
  1. Don't use $$, it's plain-TeX syntax and does not have the flexibility, for example, to move the equations automatically to the left.

  2. Don't use $$, ...

  3. Don't use $$, ...


The formatting of $$ is hard-coded in TeX, thus to change its behavior for automatic left-aligning, the source code of TeX needs to be changed.

With extra TeX code, also $$...$$ can be left aligned, but then it's cleaner to use the correct markup in the first place, where options like fleqn just work.

A. Package varwidth. Environment varwidth puts its contents into a minipage unpacks it and shrinks the contents to the maximal needed width:

\documentclass{article}
\usepackage{dsfont}
\usepackage{varwidth}
\usepackage{lipsum}
\begin{document}
\lipsum[101]

\noindent
\begin{varwidth}{\linewidth}
$$
  f(x) = \frac{\sin^2x \, \cos x}{\sin x + \cos x} -
  \frac{1}{4}\tan (\frac{\pi }{4} - x)
  \quad\forall x \in\mathds{R}
  \setminus \left\{ n\pi - \frac{\pi}{4}, n\pi + \frac{\pi}{2} \right\}, n \in \mathds{I}
$$
\end{varwidth}
\end{document}

Result <code>varwidth</code>

The vertical spacing needs some manual fixing.

B. If the equation starts with a glue, then TeX respects this glue setting assuming the user knows, what he is using.

\documentclass{article}
\usepackage{dsfont}
\usepackage{varwidth}
\usepackage{lipsum}
\begin{document}
\lipsum*[101]
$$
  \hspace{0pt minus \linewidth}
  f(x) = \frac{\sin^2x \, \cos x}{\sin x + \cos x} -
  \frac{1}{4}\tan (\frac{\pi }{4} - x)
  \quad\forall x \in\mathds{R}
  \setminus \left\{ n\pi - \frac{\pi}{4}, n\pi + \frac{\pi}{2} \right\}, n \in \mathds{I}
  \hspace{\linewidth minus -1fil}
$$
\end{document}

Result glue

The trick is based on the second \hspace with \linewidth, which forces an overfull equation. Then TeX will contribute the shrink parts, moving to the left in the first \hspace and filling the right with minus -1fil of the second \hspace.

Heiko Oberdiek
  • 271,626