TransWikia.com

How to access data properly?

TeX - LaTeX Asked by dknight on November 28, 2020

I have a class which defines the following:

% myclass.cls
newcommand*{name}[2]{def@firstname{#1}def@lastname{#2}}
newcommand*{firstname}[1]{def@firstname{#1}}
newcommand*{lastname}[1]{def@lastname{#1}}
newcommand*{familyname}[1]{def@lastname{#1}}
def@familyname{@lastname}

I am using it as follows:

documentclass[11pt, a4paper]{myclass}

%other aspects here
input{common_personal.tex}
begin{document}
value{name}

In common_personal.tex, I am providing values as follows:

name{Amitav}{Mohanty}

I expect the name to show up but value{name} does not put the value of name in the document. How can I do it correctly? I am open to suggestions around arranging files as well.

2 Answers

The fundamental misunderstanding of the OP revolves around the meaning of value. Unlike the way in which the OP seems to indicate, value does not provide the substitution text of a macro of a given name. Rather, value provides an integer representation of the argument, which needs to be the name of a counter.

A typical usage would be as part of a mathematical ifnum comparison. For example,

ifnumvalue{equation}=3relax Do something if Eq 3
else Do something if not Eq 3fi

In this case, equation is a counter associated with the equation count in a document. If we look at the name macro that the OP seems focused on, we see it is a macro, not a counter, so value{name} has no legitimate meaning.

If we look at what name actually does, it sets the values of two other macros, based upon its two arguments. The macro names where the data are stored are @firstname and @lastname. However, because the two macros have a @ in their name, they cannot, by default, be accessed by user code, because the catcode of @ during class/package definition is different (11) than it is during user code execution (12).

To access these macro values with user code, the user has two options. One is to use a csname syntax, as in csname @firstnameendcsname and csname @lastnameendcsname. The other is to change the catcode of @ (temporarily) in the user code. This latter approach is done with makeatletter to make the catcode of @=11, and makeatother to make the catcode of @=12.

The invocation of these two makeat... macros should, generally, surround the macro definition in which @ macro names are being referenced. If these @ macro names are being used directly, outside of a macro, then the makeat... commands can surround the @ macro directly, as in makeatletter@firstname{} @lastnamemakeatother.

Answered by Steven B. Segletes on November 28, 2020

You want to also define wrappers for delivering the data.

When you do name{Amitav}{Mohanty}, you are doing

def@firstname{Amitav}
def@lastname{Mohanty}

and TeX knows nothing more. As explained in Steven's answer, value is a reserved kernel command that's used to access the value of a counter.

Let's assume you need to access the name in order to print it, say as argument to author or whatever else; maybe you also need to access the last name or first name only.

You can set up some commands for the purpose. Inside myclass.cls you can do

newcommand{usedata}[1]{csname @usedata@#1endcsname}
newcommand{@usedata@fullname}{@firstnamespace@lastname}
newcommand{@usedata@firstname}{@firstname}
newcommand{@usedata@lastname}{@lastname}

so in the document you can do

usedata{fullname}
usedata{firstname}
usedata{lastname}

The idea is that usedata builds a control sequence out of its argument and you can freely define the commands for these concatenations. For instance

usedata{firstname}

will become

csname @usedata@firstnameendcsname

which is equivalent to calling @firstname as it results from the definition and here's where the first name has been stored.

Answered by egreg on November 28, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP