0

Due to my lazyness I defined a command to ease insertion of references to tables, meaning it automatically inserts "table X" instead of only "X" (see MWE below). Depending on if the reference is the first part of a new sentence or not, I would like to trigger a capital T in this string.

Is there anyway to detect in a command if this is inserted at the beginning of a sentence, in order to trigger different behaviour?

OR

How could I pass a flag to indicate this in the command manually?

I know I could also define a new command, e.g. TTABREF which just uses a capital T, but maybe I can learn something new about the power of LaTeX, e.g. bool flags in commands.

MWE below

\documentclass{report}

\newcommand{\TABREF}[1]{table \ref{#1}}

\begin{document}

\noindent A reference to \TABREF{tab:table} within a sentence is fine.
\newline \TABREF{tab:table} referenced at the beginning of the sentence would be nice to have a capital T.

\begin{table}
  \centering
  \begin{tabular}{ccc}
    A & B & C \\
    B & C & A \\
    C & A & B
  \end{tabular}
  \caption{A very nice table}    
  \label{tab:table}   
\end{table}

\end{document}

enter image description here

  • It is hard (possibly impossible) to determine accurately if a macro begins a sentence. You may be interested in the cleveref package which has similar capabilities and a discussion of this in its manual IIRC. It uses the approach of a different macro name for use when starting a sentence. – Paul Gessler Mar 25 '15 at 10:14
3

You can test the spacefactor. But warning: this works only in specific languages (English for example) where \nonfrenchspacing is set on.

\def\TABREF#1{\ifhmode \ifnum\spacefactor>2999 T\else t\fi\else T\fi able \ref{#1}}
  • For curiosity, what exactly is \spacefactor testing? Couldn't find any explicit explanation, only several related error messages xD. And if TeX is not in horiztonal mode \ifhmode=false, you simply use capital T. This would mean that its e.g. just starting a new paragraph? – HeXor Mar 25 '15 at 11:44
  • There are other places this fails, cf.~\TABREF{example}. (But +1, I think this is the best attempt I've seen.) – Paul Gessler Mar 25 '15 at 11:51
  • @HeXor Yes, \ifhmode=false means the starting of new paragraph. The \spacefactor doesn't work in this situation. And what \spacefactor means? Very roughly speaking: it includes the \sfcode of last placed character. And it does a re-calculation of inter-word spaces from default value. When English is typeset then period has \sfcode=3000, thus the space after period is greater than normal space. And \spacefactor has this value. Normal value is 1000. – wipet Mar 25 '15 at 11:54
  • Awesome, thanks alot for the detailed explanations! Works super smooth! – HeXor Mar 25 '15 at 12:05

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.