Math & ScienceEquation not centered

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Re: Equation not centered

Post by localghost »

Since this is the common way to go, his solution is absolutely reliable. Nothing to be afraid of. Your machine won't blow up.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Equation not centered

Post by cgnieder »

Cham wrote:
cgnieder wrote: You can replace this with

Code: Select all

\numberwithin{equation}{section}
It has the same effect.
Hmmm, yes. But I must admit that I'm pretty nervous about modifying my preamble, because my document is now so large and complex, I don't want to mess with this huge castle of cards.

Changing just a single line of code in the preamble may have HUGE consequences on the whole document.
Well, you don't have to but if you look at the definition of \numberwithin you'll see that it does exactly the same except for checking if the counters exist.

Code: Select all

\newcommand{\numberwithin}[3][\arabic]{%
  \@ifundefined{c@#2}{\@nocounterr{#2}}{%
    \@ifundefined{c@#3}{\@nocnterr{#3}}{%
      \@addtoreset{#2}{#3}%
      \@xp\xdef\csname the#2\endcsname{%
        \@xp\@nx\csname the#3\endcsname .\@nx#1{#2}}}}%
}
\@xp is an alias for \expandafter and \@nx another one for \noexpand.

Regards
site moderator & package author
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Equation not centered

Post by Cham »

cgnieder wrote:that it does exactly the same except for checking if the counters exist.
Hmm, this is what makes me a bit nervous. What does this implies ?

I don't think this should be a concern in my case, but...
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Equation not centered

Post by cgnieder »

If you use \numberwithin{equation}{section} it checks for the counters equation and section. Since both exist nothing happens. But as I said: you don't have to replace your code. Since it is the same it really doesn't matter.

Regards
site moderator & package author
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Equation not centered

Post by Cham »

Ok, thanks a lot for your help guys, it's really appreciated. :)

I consider my issue solved. :D

EDIT : Hmmm, not exactly. There's still a glitch, described in my next message below...
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Equation not centered

Post by Cham »

Guys, I noticed a small glitch with the previous solution. Here's again the code example which solves my previous issue, but notice the small indent there's on the first text line just after the first equation. There shoulnd't be any indent there.

Adding \noindent doesn't do anything.

What is happening here ?

Code: Select all

\documentclass{book}
\usepackage{mathtools}
\usepackage{tensor}

\newtagform{nospace}[\llap\bgroup(]{}{\egroup)}

\numberwithin{equation}{section}

\usepackage[nopar]{lipsum}% for dummy text
\begin{document}
\chapter{Blah}
\section{Blub}

\lipsum[4]
\usetagform{nospace}
\begin{equation}
  \frac{d^2 \, \displaystyle{\tensor{x}{^{\lambda}}}}{d\, \sigma^2} +
  \Gamma_{\mu \nu}^{\lambda} \; \frac{d\, \displaystyle{\tensor{x}{^{\mu}}}}{d\, \sigma} \,
  \frac{d\, \displaystyle{\tensor{x}{^{\nu}}}}{d\, \sigma} =
  \frac{d\, \displaystyle{\tensor{x}{^{\lambda}}}}{d\, \sigma} \,
  \frac{d\, }{d\, \sigma} \, \ln \sqrt{\, \tensor{g}{_{\mu \nu}} \;
  \frac{d\, \displaystyle{\tensor{x}{^{\mu}}}}{d\, \sigma} \,
  \frac{d\, \displaystyle{\tensor{x}{^{\nu}}}}{d\, \sigma}}.
\end{equation}
\usetagform{default}
\lipsum[4]
\begin{equation}
  \tensor{g}{_{\mu \nu}} \; \frac{d\, \displaystyle{\tensor{x}{^{\mu}}}}{d\, \sigma} \,
  \frac{d\, \displaystyle{\tensor{x}{^{\nu}}}}{d\, \sigma} =
  \tensor{g}{_{\mu \nu}} \; \frac{d\, \displaystyle{\tensor{x}{^{\mu}}}}{d\, \tau} \,
  \frac{d\, \displaystyle{\tensor{x}{^{\nu}}}}{d\, \tau} \,
  \Big( \, \frac{d\, \tau}{d\, \sigma} \Big)^{\! 2} \equiv
  \Big( \, \frac{d\, \tau}{d\, \sigma} \Big)^{\! 2}.
\end{equation}
\lipsum[4]

\end{document}
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Equation not centered

Post by cgnieder »

Try writing

Code: Select all

\usetagform{default}%
Notice the comment character! You might know (and if not it's good you learn now) that the end of a line is interpreted like a blank by TeX. So

Code: Select all

\usetagform{default}
Some text
is equivalent to

Code: Select all

\usetagform{default} Some text
Regards
site moderator & package author
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Equation not centered

Post by Cham »

Oh boy ! Thanks for that one. :)

Then this brings me another question, about the % at the end of the command line :

I believed that the % was used only to add some comments to the code, like this :

Code: Select all

\noindent % A comment about this command
Some text...
Now, apparently, there are other uses for that symbol ? Is that right ? I'm not sure I understand.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Equation not centered

Post by cgnieder »

Well, the comment char tells TeX to ignore the rest of the line beginning with the comment char itself. This is of course quite handy to add information to the source that's not supposed to be part of the actual document. It can however also be used to hide the and of a line. When TeX finds the end of a line it actually finds the “end of line char” which is the converted into a space (or in case an empty line follows the end of the line a \par token is inserted). This is why

Code: Select all

\usetagform{default}%
Some text
is the same as

Code: Select all

\usetagform{default}Some text
but

Code: Select all

\usetagform{default}
Some text
is the same as

Code: Select all

\usetagform{default} Some text
and

Code: Select all

\usetagform{default}

Some text
is the same as

Code: Select all

\usetagform{default}\par Some text
Regards
site moderator & package author
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Equation not centered

Post by Cham »

Ok, thanks. This is now perfectly clear.

The case is now fully solved.
Post Reply