9

I am writing a document that will need to display Common Core Standards throughout. Common Core Standards all have an abbreviation code, such as "RL.3.1" used for reference purposes.

To save time typing out the standard each time it appears, I tried using \def to define a variable, and then to call it later. Even though the variable name has numbers and decimal points, it still compiled fine. I thought that numbers and decimals were forbidden from variable names, at least in LaTeX.

\def\rl.3.1{RL.3.1: Ask and answer questions to demonstrate understanding of a text, referring explicitly to the text as the basis for the answers.}

\starttext

    \rl.3.1

\stoptext

Are numbers and decimals allowed in variable names in ConTeXt?

1
  • 3
    You have defined only a macro \rl which has to be followed by .3.1 but the “macro” is only \rl.
    – Manuel
    Commented Jun 26, 2017 at 12:36

5 Answers 5

14
+300

Only letters (with MkIV you can also use Chinese or Arabic letters) are allowed in command names but you can use other methods to have indexed strings.

The first method is \setvalue to create commands with numbers and access them afterwards with \getvalue.

\setvalue{RL.3.1}{Ask and answer questions to demonstrate understanding of
                  a text, referring explicitly to the text as the basis for
                  the answers.}

\setvalue{RL.3.2}{Recount stories, including fables, folktales, and myths
                  from diverse cultures; determine the central message,
                  lesson, or moral and explain how it is conveyed through key
                  details in the text.}

\setvalue{RL.3.2}{Describe characters in a story (e.g., their traits,
                  motivations, or feelings) and explain how their actions
                  contribute to the sequence of events.}

\starttext
\getvalue{RL.3.1}
\stoptext

Another way is the \setvariables command where you set a text for each key. The texts can then be accessed with \getvariable.

\setvariables [RL]
  [3.1={Ask and answer questions to demonstrate understanding of a text,
        referring explicitly to the text as the basis for the answers.},
   3.2={Recount stories, including fables, folktales, and myths from
        diverse cultures; determine the central message, lesson, or moral
        and explain how it is conveyed through key details in the text.},
   3.2={Describe characters in a story (e.g., their traits, motivations,
        or feelings) and explain how their actions contribute to the sequence
        of events.}]

\starttext
\getvariable{RL}{3.3}
\stoptext

If you want the key-val-method you can set the variables text with the \setvariable command.

\setvariable{RL}{3.1}{Ask and answer questions to demonstrate understanding
                      of a text, referring explicitly to the text as the basis
                      for the answers.}

Another method is to use buffers to store the text and access them later with the \getbuffer command.

\startbuffer [RL.3.1]
Ask and answer questions to demonstrate understanding of
a text, referring explicitly to the text as the basis for
the answers.
\stopbuffer

\startbuffer [RL.3.2]
Recount stories, including fables, folktales, and myths
from diverse cultures; determine the central message,
lesson, or moral and explain how it is conveyed through key
details in the text.
\stopbuffer

\startbuffer [RL.3.2]
Describe characters in a story (e.g., their traits,
motivations, or feelings) and explain how their actions
contribute to the sequence of events.
\stopbuffer

\starttext
\getbuffer[RL.3.1]
\stoptext
9

If I ask ConTeXt to \show\def, the answer is

\def=\def

which means the primitive \def is unchanged with respect to standard (Lua)TeX.

An instruction such as

\def\rl.3.1{whatever}

is perfectly valid in TeX, but it just (re)defines the control sequence \rl to have parameter text .3.1 and replacement text whatever.

In particular, TeX requires \rl to be followed by the exact four tokens

.3.1

and any other call of \rl would lead to an error

! Use of \rl doesn't match its definition.

Note that \rl .3.1 would behave the same as \rl.3.1, because spaces following a control word are ignored during tokenization.

7

How about using the variable system in ConTeXt? Your example could be translated into this:

\setvariables
  [RL]
  [3.1={Ask and answer questions to demonstrate understanding of a text,
        referring explicitly to the text as the basis for the answers.}]

\starttext

RL.3.1: \getvariable{RL}{3.1}

\stoptext
2
  • This system looks very good. How are more than one variable set?
    – Village
    Commented Jun 26, 2017 at 15:07
  • @Village Metafox's wonderful answer shows how you can do that. In short, use setvariables[RL][first-var={...}, second-var={...}, ...].
    – 9tTn9B
    Commented Jun 26, 2017 at 15:38
6

Something with plain TeX

\newcount\countA
\newcount\countB
\def\rl{\rlgobbledotA}
\def\rlgobbledotA.{\afterassignment\rlgobbledotB\countA=}
\def\rlgobbledotB.{\afterassignment\rlcontents\countB=}
\def\rlcontents{\csname rl:\the\countA:\the\countB\endcnsame}
\def\definerl.#1.#2#{\dodefinerl{#1}{#2}}
\def\dodefinerl#1#2#3{\expandafter\def\csname rl:#1:#2\endcsname{RL.#1.#2: #3}}

\definerl.3.1{Ask and answer questions to demonstrate understanding of a text, referring explicitly to the text as the basis for the answers.}
\definerl.8.17{Another number to check if you can use multiple ones.}

\rl.3.1

\rl.8.17

enter image description here

9
  • \count0? Are you sure? ;-)
    – egreg
    Commented Jun 26, 2017 at 12:53
  • @egreg No, I'm not sure hahaha Really, I just put the first thing that came to my mind. Care to explain?
    – Manuel
    Commented Jun 26, 2017 at 12:53
  • \count0 (at least in plain TeX and LaTeX) contains the current page number (and is saved in the DVI or PDF output at shipout). Maybe ConTeXt differs in this, but I'm not sure. You should allocate a specific counter.
    – egreg
    Commented Jun 26, 2017 at 12:58
  • @egreg Yes, I was lazy. In any case, are not any “user counts” so someone lazy doesn't need to allocate?
    – Manuel
    Commented Jun 26, 2017 at 12:59
  • 2
    ConTeXt provides \scratchcounter, \scratchcounterone, \scratchcountertwo and \scratchcounterthree for this case. Commented Jun 26, 2017 at 13:18
2

You could try another approach, using a switch.

\documentclass{article}

% https://tex.stackexchange.com/a/64132/123129
\usepackage{pdftexcmds}
\makeatletter
\newcommand*{\CCS}[1]{%
  \stringcases
    {#1}%
    {%
      {RL.3.1}{#1: Ask and answer questions to demonstrate understanding of a text, referring explicitly to the text as the basis for the answers.}%
      {RL.3.2}{#1: Recount stories, including fables, folktales, and myths from diverse cultures; determine the central message, lesson, or moral and explain how it is conveyed through key details in the text.}%
      {RL.3.3}{#1: Describe characters in a story (e.g., their traits, motivations, or feelings) and explain how their actions contribute to the sequence of events}%
    }%
    {[nada]}%
}
\newcommand{\stringcases}[3]{%
  \romannumeral
    \str@case{#1}#2{#1}{#3}\q@stop
}
\newcommand{\str@case}[3]{%
  \ifnum\pdf@strcmp{\unexpanded{#1}}{\unexpanded{#2}}=\z@
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
    {\str@case@end{#3}}
    {\str@case{#1}}%
}
\newcommand{\str@case@end}{}
\long\def\str@case@end#1#2\q@stop{\z@#1}
\makeatother

\begin{document}

\CCS{RL.3.1}

\CCS{RL.3.2}

\CCS{RL.3.3}

\end{document}

enter image description here

1
  • 1
    I think OP looked for a solution in ConTeXt, not LaTeX.
    – mickep
    Commented Jun 26, 2017 at 13:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .