1

Reposting as a MWE, hopefully this is minimal enough. Sorry if there's anything irrelevant.

I'm getting this error on the final '}' just before the end:

! Missing \endcsname inserted.
<to be read again> 
                   \c@questions 
l.26 }
      
The control sequence marked <to be read again> should
not appear between \csname and \endcsname.

here's the document:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{totcount}
\usepackage{pgffor}

\begin{document}
\makeatletter
\newcommand\setq[3]{%
    \global\@namedef{qa:#1.#2}{#3}%
}
\makeatother
\newtotcounter{questions}
\newcommand\newq[4]{%
    \setq{\value{questions}}{Number}{\value{questions}}
    \setq{\value{questions}}{Date}{#1}
    \setq{\value{questions}}{Time}{#2}
    \setq{\value{questions}}{Question}{#3}
    \setq{\value{questions}}{Answer}{#4}
    \stepcounter{questions}
}

\newq{268}%
{10:25}%
{Can you create a latex project with multiple documents?}%
{Yes, at least you can in Overleaf. You click on the menu in the top-left, and change main document to the tex file you want to compile.%
}

\end{document}

I'm using overleaf if that changes anything. Any optimisations I can make to the code not-related to the error is also appreciated. Thank you.

EDIT: Changed value to arabic, that's removed the endcsname error, but now I have a general compilation error.

0

1 Answer 1

1

While the value of the questions-counter is 0 the sequence \setq{\arabic{questions}}{Number}{\arabic{questions}} yields defining the control-word-token \qa:0.Number to deliver the token-sequence \arabic{questions}. That token-sequence in turn will be carried out/expanded not at the time of defining but at the time right after using/expanding the control-word \qa:0.Number, yielding the value of the question-counter current not at the time of defining but at the time of using/expanding the control-word \qa:0.Number.
You probably wish to have the sequence \arabic{questions} expanded at the time of defining \qa:0.Number.

Instead of \arabic{questions} I use \number\value{questions} because with the latter you need only one \expandafter-chain for triggering expansion until obtaining the value while with the further you need four \expandafter-chains:

 \arabic=macro:
#1->\expandafter \@arabic \csname c@#1\endcsname .
l.16 \show\arabic
                 
? i
insert> \show\@arabic
> \@arabic=macro:
#1->\number #1.
<insert>   \makeatletter\show\@arabic
                                     
l.16 \show\arabic

The first \expandafter-chain to obtain \expandafter\@arabic\csname c@questions\endcsname from \arabic{questions}.

The second \expandafter-chain to obtain \@arabic\c@questions from \expandafter\@arabic\csname c@questions\endcsname.

The third \expandafter-chain to obtain \number\c@questions from \@arabic\c@questions.

The fourth \expandafter-chain to obtain the sequence of decimal digits denoting the value of the \count-register \c@questions from \number\c@questions.

On my system the following MWE compiles without errors:

\documentclass{article}
% \usepackage[utf8]{inputenc}
\usepackage{totcount}
% \usepackage{pgffor}


\newcommand\PassFirstToSecond[2]{#2{#1}}
\newcommand\setq[3]{%
    \global\csname @namedef\endcsname{qa:#1.#2}{#3}%
}
\newtotcounter{questions}
\newcommand\newq[4]{%
    \expandafter\PassFirstToSecond\expandafter{\number\value{questions}}{\setq{\arabic{questions}}{Number}}%
    \setq{\arabic{questions}}{Date}{#1}%
    \setq{\arabic{questions}}{Time}{#2}%
    \setq{\arabic{questions}}{Question}{#3}%
    \setq{\arabic{questions}}{Answer}{#4}%
    \stepcounter{questions}%
}

\newq{268}%
{10:25}%
{Can you create a latex project with multiple documents?}%
{Yes, at least you can in Overleaf. You click on the menu in the top-left, and change main document to the tex file you want to compile.%
}

\begin{document}

\par\noindent
\csname qa:0.Number\endcsname\\
\csname qa:0.Date\endcsname\\
\csname qa:0.Time\endcsname\\
\csname qa:0.Question\endcsname\\
\csname qa:0.Answer\endcsname

\end{document}

enter image description here

You might be interested in the \name-macro. I elaborated on the \name-macro in the thread "Define a control sequence after that a space matters" which was started at TeX - LaTeX StackExchange in November 10, 2016 :

\documentclass{article}
% \usepackage[utf8]{inputenc}
\usepackage{totcount}
% \usepackage{pgffor}


\makeatletter
\newcommand\MyMacroNamePrefix@Exchange[2]{#2#1}%
\@ifdefinable\name{%
  \long\def\name#1#{\romannumeral0\MyMacroNamePrefix@innername{#1}}%
}%
\newcommand\MyMacroNamePrefix@innername[2]{%
  \expandafter\MyMacroNamePrefix@Exchange\expandafter{\csname#2\endcsname}{ #1}%
}%
\makeatother

\newtotcounter{questions}
\newcommand\newq[4]{%
    \name\name{@ifdefinable}{qa:\arabic{questions}.Number}{%
      \name\edef{qa:\arabic{questions}.Number}{\arabic{questions}}%
    }%
    \name\newcommand*{qa:\arabic{questions}.Date}{#1}%
    \name\newcommand*{qa:\arabic{questions}.Time}{#2}%
    \name\newcommand*{qa:\arabic{questions}.Question}{#3}%
    \name\newcommand*{qa:\arabic{questions}.Answer}{#4}%
    \stepcounter{questions}%
}%

\newq{268}%
{10:25}%
{Can you create a latex project with multiple documents?}%
{Yes, at least you can in Overleaf. You click on the menu in the top-left, and change main document to the tex file you want to compile.%
}

\begin{document}

\par\noindent
\name{qa:0.Number}\\
\name{qa:0.Date}\\
\name{qa:0.Time}\\
\name{qa:0.Question}\\
\name{qa:0.Answer}

\end{document}

enter image description here

You must log in to answer this question.

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