Referência no texto a um número em uma tabela

Referência no texto a um número em uma tabela

Eu queria saber se é possível fazer uma referência cruzada no texto para um número específico em uma tabela, de forma que o número no texto mude sempre que o número a que me refiro no texto muda na tabela? No meu texto, tenho repetidamente que me referir a uma soma específica numa tabela. O problema é que, como os elementos usados ​​para derivar a soma são continuamente alterados, tenho que alterar manualmente o número no texto várias vezes. Receio não poder corrigir alguns dos números do texto. Fornecendo um exemplo, suponha que a Tabela 1 tenha a seguinte forma

$$\begin{array}{|c|c|} 
 & \text{Sum}  \\ \hline
Bla bla & 5  \\ \hline
\end{array}$$

Na Tabela 1, a soma é igual a 5.

Como faço repetidamente uma referência semelhante à soma, esperava uma solução mais ou menos assim:

"A Tabela 1 mostra que a soma é igual a \ref{soma na tabela 1}"

, onde 'rotulei' o número 5 na Tabela 1 como "soma na tabela 1". (Queria apresentar um exemplo, mas não creio que seja possível). Finalmente, espero uma 'solução mais geral', visto que tenho várias tabelas 'deste tipo' na minha tese actual.

Responder1

No texto da sua pergunta você utiliza o ambiente arraypara criar uma tabela. Mas o ambiente arrayé usado para criar matrizes no modo matemático. Eu recomendo não usar mal o ambiente arraypara criar tabelas, mas sim usar o tabularambiente para criar tabelas.


Por meio do pacotezrefvocê pode facilmente introduzir seu próprio sistema de referência cruzada de forma análoga ao mecanismo \label- \refimplementado no kernel LaTeX 2ε:

O exemplo a seguir define

\StoreNumber{⟨ID/label⟩}{⟨something that in the text should denote a number⟩}

e

\RetrieveNumber{⟨ID/label⟩}


Editar em 5 de novembro de 2020:

Eu (Ulrich Diez) esqueci que “número” não implica “inteiro”.
Eu removi o \number\numexpr...\relax-thingie do \StoreNumber.

Com⟨algo que no texto deve denotar um número⟩você precisa cuidar da prevenção de expansão e da criação de strings PDF para os marcadores do arquivo .pdf.

Com o zref-things eu não usei o wrapper do zref para ativar as abreviações do babel.
Se eu fizesse isso e \RetrieveNumberacabasse como parte do argumento de um comando de seção que também pode terminar em marcadores de PDF, o wrapper do zref para abreviações de babel causaria problemas durante a criação do marcador para a seção em questão.


\documentclass{article}

\usepackage[unicode=true]{hyperref}
\usepackage[overload]{textcase}
\usepackage{zref}
\makeatletter
\@ifpackageloaded{hyperref}{%
  \@ifdefinable\RetrieveNumber@RefUndefText{%
    \DeclareRobustCommand\RetrieveNumber@RefUndefText{%
      \texorpdfstring{\nfss@text{\reset@font\bfseries ??}}{??}%
    }%
  }%
  \@ifdefinable\RetrieveNumber{%
    \DeclareRobustCommand\RetrieveNumber[1]{%
      \texorpdfstring{\zref@refused{#1}}{}%
      \zref@extractdefault{#1}{numbertostore}{\RetrieveNumber@RefUndefText}%
    }%
  }%
}{%
  \@ifdefinable\RetrieveNumber@RefUndefText{%
    \DeclareRobustCommand\RetrieveNumber@RefUndefText{%
      \nfss@text{\reset@font\bfseries ??}%
    }%
  }%
  \@ifdefinable\RetrieveNumber{%
    \DeclareRobustCommand\RetrieveNumber[1]{%
      \zref@refused{#1}%
      \zref@extractdefault{#1}{numbertostore}{\RetrieveNumber@RefUndefText}%
    }%
  }%
}%
%
\zref@newprop{numbertostore}[0]{0}%
\@ifdefinable\StoreNumber{%
  \DeclareRobustCommand\StoreNumber[2]{%
    \begingroup
    \zref@setcurrent{numbertostore}{#2}%
    \zref@labelbyprops{#1}{numbertostore}%
    \endgroup
    \RetrieveNumber{#1}%
  }%
}%
\@ifdefinable\CheckWhetherMath{%
  \DeclareRobustCommand\CheckWhetherMath{%
    \relax\ifmmode\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi 
  }%
}%
\makeatother

\usepackage{amsmath}

\usepackage[cbgreek]{textgreek} % cbgreek/euler/artemisia

\pagestyle{headings}

\begin{document}

\section{About a sum which equals \protect\NoCaseChange{\RetrieveNumber{sum in table 1}}}
\label{about a sum in table 1}

This section has the title: \nameref*{about a sum in table 1}.

\bigskip

\noindent\autoref*{table1} shows that the sum equals \RetrieveNumber{sum in table 1}.

% pi-symbol is  0x03C0 in utf-16.
% higher byte is 03 (Hex) = 3(dec) = 003(oct), 
% lower byte is C0 (Hex) = 192(dec) =  300(oct), 
% Higher byte must be marked by \9, thus the unicode-octal-triplet-pdfstring is: \9003\300

\begin{table}[h]
\centering
\begin{tabular}{|c|c|}
\hline
&\text{Sum}\\ \hline
Bla bla&\StoreNumber{sum in table 1}{\string\texorpdfstring{\CheckWhetherMath{\pi}{\textpi}}{\string\9003\string\300}}\\\hline
\end{tabular}%
\caption{A table where the sum equals \RetrieveNumber{sum in table 1}}%
\label{table1}%
\end{table}

\noindent\autoref*{table1} shows that the sum equals \RetrieveNumber{sum in table 1}.

\begin{equation}
\RetrieveNumber{sum in table 1}+\RetrieveNumber{sum in table 1}=2\cdot\RetrieveNumber{sum in table 1}
\end{equation}

\bigskip

\noindent\autoref*{table1} has the title: \nameref*{table1}

\bigskip

\hrule

\bigskip

\section{About a sum which equals \protect\NoCaseChange{\RetrieveNumber{sum in table 2}}}
\label{about a sum in table 2}

This section has the title: \nameref*{about a sum in table 2}.

\bigskip

\noindent\autoref*{table2} shows that the sum equals \RetrieveNumber{sum in table 2}.

\begin{table}[h]
\centering
\begin{tabular}{|c|c|}
\hline
&\text{Sum}\\ \hline
Bla bla&\StoreNumber{sum in table 2}{\number\numexpr(1*3)+1+1\relax}\\\hline
\end{tabular}%
\caption{A table where the sum equals \RetrieveNumber{sum in table 2}}%
\label{table2}%
\end{table}

\noindent\autoref*{table2} shows that the sum equals \RetrieveNumber{sum in table 2}.

\bigskip

\noindent\autoref*{table2} has the title: \nameref*{table2}

\begin{equation}
\RetrieveNumber{sum in table 2}+\RetrieveNumber{sum in table 2}=2\cdot\RetrieveNumber{sum in table 2}
\end{equation}

\bigskip

\hrule

\bigskip

\tableofcontents

\listoftables

\end{document}

Como o armazenamento e a recuperação de números são implementados de forma semelhante ao mecanismo \label- \refdo kernel LaTeX 2ε, você precisa compilar o documento pelo menos duas vezes até que tudo corresponda e??são substituídos pelo número correto em todo o documento. Caso o pacote hyperref esteja em uso enquanto o marcador do pacote não estiver carregado, você precisará compilar o documento pelo menos três vezes.

No meu exemplo, carreguei o pacote textcase. Ele fornece meios para evitar letras maiúsculas/minúsculas de caracteres.

Com classes de documentos como article, que \pagestyle{headings}criam automaticamente cabeçalhos de página com títulos de seção em letras maiúsculas, isso é necessário para evitar letras maiúsculas de caracteres que denotam os nomes de rótulos de referência ou números a serem recuperados. Caso contrário, a \Retrievenumber{sum in table 1}seria transformado \Retrievenumber{SUM IN TABLE 1}em cabeçalhos de página, o que seria um problema, pois (ao contrário do número "soma na tabela 1") um número "SUM IN TABLE 1" nunca foi armazenado.

insira a descrição da imagem aqui

Os marcadores são assim:

insira a descrição da imagem aqui

informação relacionada