I believe I have gather all the pieces of information I need to create what I want but yet I cannot make the connection. Let me start with the goal.
Simple Cross referencing
Let us start by creating a new notebook. Here we create a "DisplayFormulaNumbered"
cell. Enter whatever contents you want here. Then press command+J or control+J in windows and enter the tag that you want to give your equation. Here is a screenshot of what I have so far:
To be able to see the tag go to Cell > Cell Tags > Show Cell Tags
. What we want now is to refer to eq1. Here is a text cell:
Notice here that I'm using the exact $\LaTeX$ notation. What I want to do now, is to be able to select the text cell, press some button in a palette and obtain this:
Tools gathered so far
jVincent showed me here how to create the a button that executes a function to replace a selected part of the text. Notice here that I do not want to be selecting parts of text manually, I want to apply the function to the whole cell and replace the part containing ref with CounterBox["DisplayFormulaNumbered", "eq1"]
. In other words, the ref parts will tell it to put "DisplayFormulaNumbered" and the text in between the brackets next to ref is the tag.
To find the cases where I have ref we can use something like this:
StringCases["Reference \\ref{tag1} goes here. Reference \\ref{tag2} \here.", "\\ref{" ~~ __ ~~ "}"]
{"\\ref{tag1} goes here. Reference \\ref{tag2}"}
Unfortunately, I'm not quite making a good use of it. But the idea is to maybe use something like this to obtain the tag. Next we have to check if this is a valid tag in the notebook (given that we were able to locate the tag in the whole string):
MemberQ[NotebookTools`NotebookCellTags[EvaluationNotebook[]], "tagName"]
This is thanks to Rojo's answer. I feel a little ashamed that I cannot combine the tools that you guys have provided me yet but I'd rather get this out of the way so that I can be more efficient when doing cross referencing. What do you guys think? Is this worth the trouble of creating a palette button that does this?
EDIT:
The idea I was suggesting for making the shortcut is by no means what I wanted to see. I guess I should have clarified that really all I wanted to see was an easy and fast method of dealing with cross referencing. Albert Retey has shown two methods in his answer and makes a good point that if you were to keep typing large chunks of text and evaluate everything in one go then my suggestion would be justified. In any case, I'm satisfied with Retey's method but other neat ideas are still welcomed.