[Tex/LaTex] Insert labels inside table rows

cross-referencinglabelslyxtables

I have a long table with different equation, I need to make a label and then refer to them in some cells, trying to do that by putting usual label giving me wrong results in LyX, more precisely if the table is in some section, strangely enough making a label in some cell, and referencing it somewhere, will make a link not to the cell's label, but to the section title it self.

Below is exported code from LyX to Plain LaTeX:

%% LyX 2.1.2 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\usepackage{float}
\usepackage[unicode=true,pdfusetitle,
 bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
 breaklinks=false,pdfborder={0 0 0},backref=false,colorlinks=false]
 {hyperref}
\usepackage{breakurl}
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}
\makeatother
\begin{document}
\section{A New Section}
\begin{table}[H]
\begin{centering}
\begin{tabular}{|c|c|}
\hline 
\label{eq:Mass Energy}$E=mc^{2}$ & \tabularnewline
\hline 
\end{tabular}
\par\end{centering}

\protect\caption{sample}
\end{table}
A reference to (\ref{eq:Mass Energy})
\end{document}

I'm not sure if this LyX bug or LaTeX specific, any ideas? (especially by using labels not hyperref).

PS:

  1. Interestingly, If I totally will remove the section, the output of the reference became empty.
  2. There are some similar questions here, like auto numbering with labelling table rows, but that is not what I need.

Best Answer

\label records the last referencable item, which is the section head in your case.

Normally to refer to a formula you would use equation or align but sometimes in tables you can do it "by hand"

enter image description here

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\usepackage{float}
\usepackage[unicode=true,pdfusetitle,
 bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
 breaklinks=false,pdfborder={0 0 0},backref=false,colorlinks=false]
 {hyperref}
\usepackage{breakurl}
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}
\makeatother
\begin{document}
\section{A New Section}
\begin{table}[H]
\begin{centering}
\begin{tabular}{|c|c|}
\hline 
\refstepcounter{equation}\label{eq:Mass Energy}$E=mc^{2}$ & (\theequation)\tabularnewline
\hline 
\end{tabular}
\par\end{centering}

\protect\caption{sample}
\end{table}
A reference to (\ref{eq:Mass Energy})
\end{document}