3

I'm trying to reference equation by \eqref when I use different tags form throughout document, but the output can't display the tag form that I want. As mentioned in mathtools package usage, "be careful" in this situation.
The display equation I want is "c+d=e [1-3]" but it only work in equation environment. The reference I want is "There is a test in (1-2), [1-3], [1-4] and [1-5]." So

1. How can I get the result I say above ?
2. If I don't change those code in math mode, how can I custom the command or use which package to display correct tag form and reference ?
3. Why does the equation environtment display different from other math environment?

I'm using XeLaTeX under texlive2015.

Here is my code:

\documentclass{article}
\usepackage[pdfborder=0 0 0]{hyperref} % must needed
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}

\newtagform{real}{[}{]} 
\renewcommand{\theequation}{\arabic{subsection}--\arabic{equation}}
\newcommand{\zzreal}{\usetagform{real}\stepcounter{equation}\tag{\theequation}} %

\section{Sample}
\subsection{subsample}
\begin{align}
a+b &=c\\
b+c &=d  \label{eq:2}\\
c+d &=e \zzreal \label{eq:3}
\end{align}
\begin{gather}
c+d =g  \zzreal\label{eq:4}
\end{gather}
\begin{equation}
d+e =f \zzreal\label{eq:5}
\end{equation}

There is a test  in \eqref{eq:2}, \eqref{eq:3} , \eqref{eq:4} and  \eqref{eq:5} .

\end{document}

In addition, I have another code to satisfy the 1st question but not the requirement of 2nd question i.e I insert a command after each equation no matter whether I refer them or not. However, it seems inconvenient when writing the equation. Any workaround to optimize this code ?
Desired Result code:

\documentclass{article}
\usepackage[pdfborder=0 0 0]{hyperref} % must needed
\usepackage{amsmath}
\usepackage{mathtools}

\renewcommand{\theequation}{\arabic{subsection}--\arabic{equation}}
\new} command{\zzreal}{\refstepcounter{equation}\tag*{[\theequation]}}%
\newcommand{\zzsim}{\stepcounter{equation}\tag*{(\theequation)}}

\begin{document}
\section{Sample}
\subsection{subsample}
\begin{align}
a+b &=c  \zzsim \\
b+c &=d & \zzsim \label{eq:2} \\
c+d &=e &\zzreal \label{eq:3}
\end{align}
\begin{gather}
c+d =g  \zzreal\label{eq:4}
\end{gather}
\begin{equation}
d+e =f \zzreal\label{eq:5}
\end{equation}

There is a test  in \ref{eq:2}, \ref{eq:3}, \ref{eq:4} and  \ref{eq:5} .

\end{document}
4
  • gather and align process each cell and line in a group, so when the tag is typeset, the setting of the tag form has been forgotten.
    – egreg
    Dec 24, 2015 at 15:10
  • @egreg Thanks your answer for the 3rd question.
    – Menhong
    Dec 24, 2015 at 16:58
  • Why do you want this strange behaviour?
    – vonbrand
    Dec 25, 2015 at 15:35
  • @vonbrand As I want to distinguish the empirical formula and constitutive equations by different tag form when output the pdf file.
    – Menhong
    Dec 26, 2015 at 2:28

1 Answer 1

4

When you use \tag the contents will be saved by \label. So if you put the () or [] INSIDE the tag and remove the () outside, the reference will match (using \ref instead of \eqref). The downside is that you have to manually tag every equation (or get a naked equation number).

\documentclass{article}
\usepackage[pdfborder=0 0 0]{hyperref} % must needed
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}

\renewcommand{\theequation}{\arabic{subsection}--\arabic{equation}}
\newtagform{naked}{}{} 
\usetagform{naked}
\newcommand{\ptag}{\refstepcounter{equation}%
  \tag{$($\theequation$)$}}
\newcommand{\btag}{\refstepcounter{equation}%
  \tag{$[$\theequation$]$}}

\section{Sample}
\subsection{subsample}
\begin{align}
a+b &=c \ptag\\
b+c &=d \ptag \label{eq:2}\\
c+d &=e \btag \label{eq:3}
\end{align}
\begin{gather}
c+d =g  \btag \label{eq:4}
\end{gather}
\begin{equation}
d+e =f \btag\label{eq:5}
\end{equation}

There is a test  in \ref{eq:2}, \ref{eq:3} , \ref{eq:4} and  \ref{eq:5} .

\end{document}

reference with brackets

Note: I first tried to put the [] into \theequation, but amsmath ignores changes to \theequation in order to implement \tag.

While it is no longer used here, I am including the hyperref version of \mylabel for posterity.

\makeatletter
\newcommand{\mylabel}[2]% #1 = label name, #2 = text for \ref
{\protected@write\@auxout{}{\string\newlabel{#1}{{#2}{\thepage}{\@currentlabelname}{\@currentHref}{}}}}
\makeatother
2
  • Thanks for the answer. I may prefer to use one reference command for both brackets tags and parenthesis tags thus I needn't care about which label is for backets one. The code you post reveals that I couldn't \ref{eq:1} to get the parenthesis if I label the equation (1-1).
    – Menhong
    Dec 25, 2015 at 12:04
  • I had an AHA! moment and completely revised my solution. Dec 26, 2015 at 13:04

You must log in to answer this question.

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