Math & ScienceEquation not centered

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Equation not centered

Post by Cham »

Is there a way to "smash" the tag number ?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Equation not centered

Post by Stefan Kottwitz »

Most can be redefined and patched, also tagging macros. Just be aware, that redefining internal macros can mean that it doesn't work with future packages releases which decide to change internal macros. Well, it would work now. So, one way of horizontal smashing is using \makebox with a value of 0pt. With 0pt we get a line break though, but let's choose the smallest TeX unit and it works. Just insert right before your \makeatother:

Code: Select all

\renewcommand*{\maketag@@@}[1]{\hbox{\m@th\normalfont{\makebox[1sp][r]{#1}}}}
Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Equation not centered

Post by Cham »

Thanks Stefan, this works.

Unfortunately, it may cause problems with lots of other equations (I didn't checked yet).

Is there a way to achieve the same effect on a particular equation, without affecting the others ?
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Equation not centered

Post by Cham »

OUCH !

Stefan, your modification affects all the equation references in the text. Just add a \label{} to an equation and use \eqref{} in the text, and it will be messed-up !
Last edited by cgnieder on Sun Oct 14, 2012 4:35 pm, edited 1 time in total.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Equation not centered

Post by cgnieder »

How about this: use mathtools instead of amsmath (the first loads the latter anyway) and define a tagform nospace:

Code: Select all

\newtagform{nospace}[\llap\bgroup(]{}{\egroup)}
I could imagine that this works for all tags but even if it doesn't you could switch back and forth with \usetagform.

Here is a complete example (also note that I change the equation counter with amsmath's \numberwithin):

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}
Regards
site moderator & package author
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Re: Equation not centered

Post by cgnieder »

Note: this will also affect a possible reference to the equation.
site moderator & package author
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Equation not centered

Post by Cham »

Thanks cgnieder, your solution appears to work very well.

Since I'm already using the mathtools package in my real document, I may simply add this code to the preamble :

Code: Select all

\newtagform{nospace}[\llap\bgroup(]{}{\egroup)}
just under my usual tag modification :

Code: Select all

\makeatletter
\renewcommand\theequation{\thesection.\arabic{equation}}
\@addtoreset{equation}{section}
\makeatother
Then using \usetagform{nospace} and \usetagform{default} around a special equation solves the issue for that particular equation. All other equations are left untouched.
Last edited by Cham on Sun Oct 14, 2012 4:55 pm, edited 2 times in total.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Equation not centered

Post by cgnieder »

Cham wrote:just under my usual tag modification :

Code: Select all

\makeatletter
\renewcommand\theequation{\thesection.\arabic{equation}}
\@addtoreset{equation}{section}
\makeatother
You can replace this with

Code: Select all

\numberwithin{equation}{section}
It has the same effect.

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: 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.
Last edited by Cham on Sun Oct 14, 2012 5:09 pm, edited 1 time in total.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Equation not centered

Post by Cham »

Since I'm no expert in LaTeX coding : Before adopting cgnieder's solution, I would like to hear comments from the other "pros" here about his solution.

Is there any inconvenience about that solution ?

Apparently, it's working very well in my document (but I didn't checked all of the 700 pages yet!), and as I said in my previous message, I'm more and more reluctant to modify my preamble (it's now a huge "castle of cards").
Post Reply