3

I have an array environment that is used to present inference rules (via a custom command). Now, these inference rules have names and i would like to refer to them using \label and \cref commands. I am guessing this is possible though the details elude me.

As a small example, this is the command:

\newcommand{\typerule}[3]{\ensuremath{\begin{array}{c}\textit{\scriptsize ({#1})} \\#2 \\\hline{\ensuremath{#3}}\end{array}}}

It produces an inference rule with hypotheses (#2), a line, a conclusion (#3), all topped by a name for the rule (#1).

My idea would be to add another parameter and something \label-style related to it, so that in the text later on i can use cleveref's \cref{} and then the name saved in the environment (and the related category name) pop up.

So i would write

\typerule{name}{premise}{conclusion}{labelidentifier}

and somewhere else

The \cref{labelidentifier} does ...

and the produced output should be something like

The rule name does

where rule is generated by cleveref and name is the one i specified previously.

  • Since you need to pick up a string of characters, not a counter, may be Heiko Overdiek’s zref-titleref, \gettitlestring and nameref have more to offer. – Sveinung Sep 12 '14 at 9:45
  • Well, @Sveinung, these are names i have not heard of before, i'll investigate, thanks! (working answers using them are still welcome) – Squera Sep 12 '14 at 10:04
3

Trick cleveref into thinking that “name” is the number.

\documentclass{article}
\usepackage{array,booktabs,cleveref}

\newcounter{typerule}
%%% standard format
%\crefname{typerule}{rule}{rules}
%%% more complex form
\crefformat{typerule}{#2rule~\textit{#1}#3}
\Crefformat{typerule}{#2Rule~\textit{#1}#3}

\newcommand{\typerule}[4]{%
  \def\thetyperule{#1}%
  \refstepcounter{typerule}%
  \label{#4}%
  \begin{tabular}{c}
  \scriptsize (\textit{#1}) \\
  $#2$ \\
  \midrule
  $#3$
  \end{tabular}%
}

\begin{document}

\typerule{modus ponens}{A\to B\quad A}{B}{MP}

\bigskip

The \cref{MP} does something.

\end{document}

I've also changed somewhat your command, in order to remove the acrobatic parts with \ensuremath and adding a better spacing with \cmidrule.

enter image description here

  • Woah, that works (almost) perfectly! I still had to fix some of my math stuff that I sent as arguments. But many many thanks!! Just a clarification: the new counter is needed because it's a new "type" of reference, right? While the \def tells what to use as a name? the \def is the most obscure part. Also, what code is using the "booktabs" package? – Squera Sep 12 '14 at 10:22
  • 1
    The label-ref mechanism, which is enhanced by cleveref, uses the counter's representation stored in \the<counter>, in this case \thetyperule. A \refstepcounter is needed in order to start the business and make \label do its work of writing the desired annotations in the .aux file; this will use whatever the current counter's representation is. – egreg Sep 12 '14 at 10:28
  • The impossibel is solved in an hour, the miracles take a little longer ... =:-) – Sveinung Sep 12 '14 at 11:30

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.