views:

29

answers:

1

I'm writing a thesis and have been searching on many occasions trying to find a solution to my programming problem. Basically, I have a series of items that I've distinguished in my research data as "A1", "A2", "A3", … , "A13", "B1", B2", and so on. These data labels, by the way, I can't change now because it's been used throughout my thesis. They are always formatted as [caps-letter][digit(length of 1 to 2 chars)], e.g., X20 or L9. For each data item, I want to assign a specific name. Because LaTeX doesn't allow numbers in the command, I have already created a LONG list of the following types of commands to assign names to each data label:

\newcommand{\DataNameAi}[]%
 {Data name for A1}
\newcommand{\DataNameAii}[]%
 {Data name for A2}
% …
\newcommand{\DataNameXxi}[]%
 {Data name for X11}
% …

and so on. Basically, as you can see I've named the command as "\DataName" followed by the letter (in caps), followed by the number written out as roman numerals. This was all manually done, and I did this only because LaTeX didn't seem to like any arabic numbers in the command name. If it permitted this, I would have used \DataNameA1 etc.

Elsewhere, I also have a command to reference the data specifically:

\newcommand{\GotoData}[1]%
 {\hyperref[data#1]{Data~#1}}
See data at \Gotodata{E10} % this links to another location labelled \label{dataE10}

Now, I want to now assign a latex command that can take just one variable, the data label (whether it's "Q30" and "A3"), and use the \GotoData command as well as bring up the corresponding data name in the \DataName*** command. That is, type \CompleteData{E10}, for example, and then have LaTeX load something like:

"This is [Data E10] named [Data name for E10]."

This means the command might look something like:

\newcommand{\CompleteData}[1]%
{This is [\GotoData{#1}] named [\DataNameEx].}

\CompleteData{E10} % <--- this should look like "This is [Data E10] named [Data name for E10]."

As you can see, the code above is incomplete: I'm stuck with how to use the #1 variable to generate the necessary \DataName*** command within the \CompleteData newcommand.

So very basically, I see the only way as achieving this result is to have the code extract and convert the last number (one to two digits long) into a roman numeral. Specifically, I've been trying to figure out how to do a few things:

  1. how to extract just the digits from the end of a parameter in a newcommand (such as the digits in my "Q31" or "A1" parameters).
  2. similarly extract the letter from the first character of the parameter
  3. how to convert numbers to numerals

I've tried searching in many different ways but never seem to find what I need to answer these two questions … I thought I was close when I found this site but later realised it's not what I'm after. The etextools LaTeX package also looked promising but I'm too novice (not even a programmer) to make much sense out of the help PDF that comes with my TexLive (2010) installation. I've also read about \roman and \romannumeral (e.g., here) but those two commands cause errors when I compile for some reason. On my computer, \roman{2} becomes "roman" while \romannumeral{2} becomes "2". Just don't understand how they work.

Any guidance, demonstration code, or hints would be greatly appreciated! Thank you.

A: 

Here's an example that works for me:

\documentclass{article}
\usepackage{hyperref}

\newcommand{\DataNameAii}{Data name for A2}
\newcommand{\GotoData}[1]{\hyperref[data#1]{Data~#1}}

\newcommand{\CompleteData}[1]{This is [\GotoData{#1}] named [\FormatDataName#1$].}
\newcounter{DataNumber}
\def\FormatDataName#1#2${\setcounter{DataNumber}{#2}\csname DataName#1\roman{DataNumber}\endcsname}

\begin{document}
\section{Data A2}\label{dataA2}
\CompleteData{A2}
\end{document}

\FormaDataName extracts the first character into #1, and the number into #2. It does that using the fact that \FormatDataName takes a delimited argument (delimited by a final $). After that, it's just a case of constructing the macro name you want to call (using \csname), and using \roman to format the number as roman numerals. (I think the reason you couldn't get this to work is because you weren't passing \roman a counter).

Tom
Hi Tom, thank you so much for your reply. Your code works perfectly for my purposes. I just wish I . No idea how it works but it does. I'm now trying to figure out how to "accept your answer", Tom, and give you your deserved 15 points. Thank you again. :)
stahmo