5

I'm coming to you because I have a problem when I am redefining commands.

In this case, I'm trying to change the numbering of the mathematical equations. The problem is that a space is added before the "Eq" and before the reference "a" (in bold) as shown below.

Screenshot of the problem

I looked on the internet for a solution to this problem. So I have put in "\ignorespaces" but the problem remains...

\documentclass{book}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[leqno]{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{lipsum} 
\usepackage[french]{babel}
\usepackage[showframe]{geometry} 


\makeatletter
    \renewcommand\theequation{
        \ignorespaces\@alph\c@equation
    }
    \renewcommand\tagform@[1]{
        \maketag@@@{
            \ignorespaces Eq.~\ignorespaces#1.\unskip\@@italiccorr
        }
    }

    % reference
    \renewcommand{\eqref}[1]{
        \ignorespaces\ref{#1}
    }
\makeatother

\begin{document}

\chapter{First chapter}
\section{Section 1}

\lipsum[1]

\begin{align}
    f(x) = x^2 + \frac{1}{\sqrt[3]{x}}
    \label{eq1} 
\end{align}
\textbf{See equation \ref{eq1}.}
\lipsum[2]
\begin{align}
    g(x) = x^3 + \frac{4}{\sqrt{x}}
    \label{eq2} 
\end{align}

\end{document}

Do you know how to fix it? I'm sure it's a silly solution, but right now it eludes me.

1
  • 2
    Welcome to TeX.SE. Your English is definitely good enough to be understood. :-)
    – frougon
    Commented Mar 7, 2020 at 16:58

2 Answers 2

7

Needless to reinvent the wheel: you can do that with the \newtagform command, from mathtools, which loads amsmath.

\documentclass{book}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[leqno]{mathtools}
\usepackage{lipsum}
\usepackage[french]{babel}
\usepackage[showframe]{geometry}
\renewcommand{\theequation}{\alph{equation}}
\newtagform{alph}[Eq. ]{}{}
\usetagform{alph}

\begin{document}

\chapter{First chapter}
\section{Section 1}

\lipsum[1]

\begin{align}
    f(x) = x^2 + \frac{1}{\sqrt[3]{x}}
    \label{eq1}
\end{align}
\textbf{See equation \ref{eq1}.}

\section{Section 2}
\lipsum[2]
\begin{align}
    g(x) = x^3 + \frac{4}{\sqrt{x}}
    \label{eq2}
\end{align}

\end{document} 

enter image description here

6

Edit after @egreg's comment: (Thanks @egreg)

\documentclass{book}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[leqno]{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{lipsum} 
\usepackage[french]{babel}
\usepackage[showframe]{geometry} 


\makeatletter
    \renewcommand\theequation{%
        \@alph\c@equation
    }
    \renewcommand\tagform@[1]{%
        \maketag@@@{%
        Eq.~\ignorespaces#1.\unskip\@@italiccorr
        }%
    }

    % reference
    \renewcommand{\eqref}[1]{%
        \ref{#1}%
    }
\makeatother

\begin{document}

\chapter{First chapter}
\section{Section 1}

\lipsum[1]

\begin{align}
    f(x) = x^2 + \frac{1}{\sqrt[3]{x}}
    \label{eq1} 
\end{align}
\textbf{See equation \ref{eq1}.}
\lipsum[2]
\begin{align}
    g(x) = x^3 + \frac{4}{\sqrt{x}}
    \label{eq2} 
\end{align}

\end{document}

Add some %s in the end of the lines like below:

If you leave them without been commented these tokens are supposed to be spaces.

\documentclass{book}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[leqno]{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{lipsum} 
\usepackage[french]{babel}
\usepackage[showframe]{geometry} 


\makeatletter
    \renewcommand\theequation{%
        \ignorespaces\@alph\c@equation
    }
    \renewcommand\tagform@[1]{%
        \maketag@@@{%
            \ignorespaces Eq.~\ignorespaces#1.\unskip\@@italiccorr
        }%
    }

    % reference
    \renewcommand{\eqref}[1]{%
        \ignorespaces\ref{#1}%
    }
\makeatother

\begin{document}

\chapter{First chapter}
\section{Section 1}

\lipsum[1]

\begin{align}
    f(x) = x^2 + \frac{1}{\sqrt[3]{x}}
    \label{eq1} 
\end{align}
\textbf{See equation \ref{eq1}.}
\lipsum[2]
\begin{align}
    g(x) = x^3 + \frac{4}{\sqrt{x}}
    \label{eq2} 
\end{align}

\end{document}

The above code should work

4
  • 2
    The only useful \ignorespaces is the one in front of #1.
    – egreg
    Commented Mar 7, 2020 at 16:54
  • Thanks @egreg ... Fixed.
    – koleygr
    Commented Mar 7, 2020 at 16:56
  • Thank you for your prompt response! I didn't know that Latex adds a space to each new line in the definition of a command. Yes indeed, Egreg, I've been putting \ignorespace all over the place in the hope of solving my problem. . . Anyway, thank you, my problem is solved.
    – guest
    Commented Mar 7, 2020 at 17:02
  • Welcome @guest ... Many of us had similar problems once ... Take a look here: tex.stackexchange.com/q/376953/120578 a post that could help you understand this kind of tokens and caused spaces.
    – koleygr
    Commented Mar 7, 2020 at 17:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .