# Automatizing cross-reference insertion in #LaTeX

When you are writing a paper (or other documents) in LaTeX, you constantly need to refer to figures, tables, section and etc. throughout your document with \ref{label} command. However, this only gives you a counter and does not convey the type of the object being referred, e.g. figure, table or section, so you have to prefix to it a descriptive string, e.g. Figure~\ref{label} or Eq~(\ref{label}). and you have to do it over and over throughout your documents each time encounter a cross-reference. Thus it is better to automatize this repetitive procedure.

LaTeX user-defined macros

A common but not standard remedy to ease this procedure is to define LaTeX macros like:

\newcommand{\Sref}[1]{Section~\ref{#1}}

and use \Sref abbreviations instead of Section~\ref. Nevertheless, you need to either add a set of similar user-defined macros to each of your documents, which makes it lengthy and reduces readability, or put it in a separate file and load it from different documents, which make your document less portable and will cause problems when your writing in collaboration with other people.

Fancy cross-referencing

Another option is to use "fancyref" packages that performs in the similar way by defining customizable macros. You can download it from CTAN or from your LaTeX distribution. To use this package you to follow a simple rule for your labeling: ⟨prefix⟩⟨delim⟩⟨label⟩, in which <prefix> is fig for figure, tab for tables, sec for section and so on, <delim> is ":" by default and <label> is the normal label name. Therefore instead of Section~\ref{label} you can use \Fref{sec:label} or \fref{sec:label} for lower-case section~\ref{label}.

*NOTE*: Although this labeling convention is a bit restrictive, it is very useful to distinguish what you are referring to even if you are not using fancyref package. Here is some common prefixes for labeling of different objects:

Text-editor's user-defined macros

All the above mentioned options define LaTeX macros, thereby impair the portability of your LaTeX and also reduce its readability. A better solution is to utilize your text-editor's macros instead. In this way you can define some abbreviations (or key bindings) that the editor translates it to the complete string. Setting this option vary a lot from one editor to another, but I give instruction for some common tex bundles.

TexShop:

If your are using #Mac OS, you are most probably familiar with TexShop. This TeX previewer includes a macro for inserting cross-references (with key binding of Command+Shift+R) that search the entire document to find the labels (define by \label{}) and let you select what you want to refer from a list. Following the aforementioned labeling convention, you can narrow down the list by typing the prefix, selecting it and then pressing the keybinding. This macro only insert \ref{label} but you can extend this capability to prefix the descriptive string by modifying the macro as follow:

From the menu bar select Macros>Open Macro Editor... then from the left panel of the Macro Editor select "insert reference" that will show the content of this macro in the Content panel. In the Content panel find (around the end) the lines containing:

if ref_command = "" then
    set ref_insert to label_insert
else
    set ref_insert to "\\" & ref_command & "{" & label_insert & "}"
end if

and replace it with

if ref_command = "" then
    set ref_insert to label_insert
else
    set label_type to label_insert as string
    if label_type starts with  "eq:" then
        set ref_insert to "Eq.~(\\" & ref_command & "{" & label_insert & "})"
    else if label_type starts with "fig:" then
        set ref_insert to "Figure~\\" & ref_command & "{" & label_insert & "}"
    else if label_type starts with "tab:" then
        set ref_insert to "Table~\\" & ref_command & "{" & label_insert & "}"
    else if label_type starts with "sec:" then
        set ref_insert to "Section~\\" & ref_command & "{" & label_insert & "}"
    else if label_type starts with "chap:" then
        set ref_insert to "Chapter~\\" & ref_command & "{" & label_insert & "}"
    else
        set ref_insert to "\\" & ref_command & "{" & label_insert & "}"
    end if
end if

You can also replace all the content of the macro with the contents of the attached file (InsertRefMacroTexShop.txt).