8

The following MWE procudes this result:

eq

As you see, I want to have tiny equation numbers/tags, but currently it is not centered vertically of course (as the rule shows), it lies on the baseline. How can I possibly change this? That is, I want to print (1) a bit higher.

(NB: I do not want to change the formatting of \ref, \eqref, \cref though).

Thanks for your ideas!


\documentclass[11pt, a4paper]{article}
\usepackage{xcolor}
\usepackage{mathtools}
\usepackage{hyperref}
\usepackage{cleveref}
\newtagform{myTag}{%
    \fontsize{5}{0}\sffamily\selectfont\color{green}(}{)}
\usetagform{myTag}

\makeatletter
\renewcommand*{\eqref}[1]{%
  \hyperref[{#1}]{%
    \textup{(\ref{#1}})
  }%
}
\makeatother

\begin{document}
test :
\begin{equation}\label{fun_eq}
x^2+y^2+z^2 + x^2+y^2+z^2 + x^2+y^2+z^2 =
3(x^2+y^2+z^2)
\mathllap{\rule[2.6pt]{12cm}{0.2pt}\hspace{-1.5cm}}
\end{equation}
My favorite equation is \ref{fun_eq}, \eqref{fun_eq}, \cref{fun_eq}
\end{document}
5

The TeX primitive \vcenter centers boxes vertically around the math axis (corresponding to the line you have drawn with a bit of trial/error, I presume) without having to guess the right amount.

Since the parentheses have a larger height and a depth than the numbers, the concept of vertical centering is relative: should the whole tag (including parentheses) be vertically centered, or only the number? I show both possibilities; I have also fixed your redefinition of \eqref a bit. (I've changed the color to make the screenshot clearer; green on white is difficult to read.)

\documentclass[11pt, a4paper,twocolumn]{article} % twocolumn for smaller snapshot
\usepackage{xcolor}
\usepackage{mathtools}
\usepackage{hyperref}
\usepackage{cleveref}

\newtagform{myTagA}% vertically centered on parentheses
   {%
    $\vcenter\bgroup
    \hbox\bgroup\bgroup % double group to avoid color leaking
    \fontsize{5}{0}\sffamily\selectfont\color{blue}%
    (%
   }{%
    )%
    \egroup\egroup
    \egroup$%
   }

\newtagform{myTagB}% vertically centered on number
   {%
    $\vcenter\bgroup
    \hbox\bgroup\bgroup % double group to avoid color leaking
    \fontsize{5}{0}\sffamily\selectfont\color{blue}%
    \smash{(}%
   }{%
    \smash{)}%
    \egroup\egroup
    \egroup$%
   }

\renewcommand*{\eqref}[1]{%
  \hyperref[{#1}]{%
    \textup{(\ref*{#1}})%
  }%
}

\begin{document}

test : \usetagform{myTagA}
\begin{equation}\label{fun_eq}
\mathrlap{\rule[2.6pt]{5cm}{0.2pt}}
a + b = c
\end{equation}
My favorite equation is \ref{fun_eq}, \eqref{fun_eq}, \cref{fun_eq}
test : \usetagform{myTagB}
\begin{equation}
\mathrlap{\rule[2.6pt]{5cm}{0.2pt}}
a + b = c
\end{equation}

\end{document}

enter image description here

Here a closer snapshot of the equation numbers:

enter image description here

5
  • OK, I see, I should have used something like \mathrlap{\vcenter{ \rule{12cm}{0.2pt} }} ;-). Thanks!
    – Watson
    Mar 12 at 17:30
  • 1
    @Watson Not quite. \vcenter expects vertical material but \rule is a LaTeX macro which expands to a rule in horizontal mode, so the result wouldn't be correct. You need \mathrlap{\vcenter{\hrule width 12cm}}.
    – campa
    Mar 12 at 17:37
  • Thanks for taking the time to explain to me these things! So hrule is used in "vertical mode"? (I think the "h" in "hrule" is just reminds us that hrule prints a horizontal line, which has apparently nothing to do with horizontal mode).
    – Watson
    Mar 12 at 17:48
  • 1
    @Watson Yes, that's a very strange thing in TeX: \hrule is a vertical mode comand, and \vrule works in horizontal mode. The idea is that if you do \vbox{\hrule\hbox{...}} then the \hrule will automatically stretch to the natural width of the \vbox if no explicit parameters are given. Of course, if you declare a larger height than with the rule will look like a vertical one, but in TeX's eyes that's irrelevant.
    – campa
    Mar 12 at 17:52
  • Thanks for your explanations. I have still a lot to learn in (La)TeX!
    – Watson
    Mar 12 at 18:59
3

Here is a solution using environ:

\usepackage{environ}
\NewEnviron{LiftBox}{\raisebox{.3em}{\BODY}}

\newtagform{myTag}{%
    \fontsize{5}{0}\sffamily\selectfont\color{green}\begin{LiftBox}(}{)\end{LiftBox}}
\usetagform{myTag}

enter image description here

Complete example:

\documentclass[11pt, a4paper]{article}
\usepackage{xcolor}
\usepackage{mathtools}
\usepackage{hyperref}
\usepackage{cleveref}

\usepackage{environ}
\NewEnviron{LiftBox}{\raisebox{.3em}{\BODY}}

\newtagform{myTag}{%
    \fontsize{5}{0}\sffamily\selectfont\color{green}\begin{LiftBox}(}{)\end{LiftBox}}
\usetagform{myTag}

\makeatletter
\renewcommand*{\eqref}[1]{%
  \hyperref[{#1}]{%
    \textup{(\ref{#1}})
  }%
}
\makeatother

\begin{document}
test :
\begin{equation}\label{fun_eq}
x^2+y^2+z^2 + x^2+y^2+z^2 + x^2+y^2+z^2 =
3(x^2+y^2+z^2)
\mathllap{\rule[2.6pt]{12cm}{0.2pt}\hspace{-1.5cm}}
\end{equation}
My favorite equation is \ref{fun_eq}, \eqref{fun_eq}, \cref{fun_eq}
\end{document}
1
  • Great! There is some manual adjustment, but at least this solution is easy to understand! Thanks :-)
    – Watson
    Mar 12 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.