缺少插入 \endcsname(使用 MWE 重新发布)

缺少插入 \endcsname(使用 MWE 重新发布)

以 MWE 身份重新发布,希望内容足够简洁。如果有不相关的内容,请见谅。

我在即将结束之前的最后一个“}”上收到此错误:

! 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.

这是文件:

\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}

如果有任何变化,我会使用 overleaf。如果能对与错误无关的代码进行任何优化,我也将不胜感激。谢谢。

编辑:将值更改为阿拉伯语,这样就消除了 endcsname 错误,但现在我遇到了一般的编译错误。

答案1

而 -counter 的值questions0序列\setq{\arabic{questions}}{Number}{\arabic{questions}},定义控制字标记\qa:0.Number以传递标记序列\arabic{questions}。该标记序列反过来将不是在定义时执行/扩展,而是在使用/扩展控制字之后立即执行/扩展\qa:0.Number,产生 -counter 的值,question不是在定义时,而是在使用/扩展控制字时\qa:0.Number。您可能希望在定义时扩展
序列。\arabic{questions}\qa:0.Number

\arabic{questions}我使用而不是,\number\value{questions}因为使用后者您只需要一个\expandafter-chain 来触发扩展直到获得值,而使用进一步的扩展您需要四个\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

\expandafter获得第一个-chain 。\expandafter\@arabic\csname c@questions\endcsname\arabic{questions}

从中\expandafter获得的第二个链。\@arabic\c@questions\expandafter\@arabic\csname c@questions\endcsname

从中\expandafter获得的第三个链。\number\c@questions\@arabic\c@questions

第四个-链从中获取表示-寄存器\expandafter的值的十进制数字序列。\count\c@questions\number\c@questions

在我的系统上,以下 MWE 编译时没有错误:

\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}

在此处输入图片描述

您可能对 -macro 感兴趣\name。我\name在主题中详细阐述了 -macro“定义一个控制序列,之后有一个空格”该活动于 2016 年 11 月 10 日在 TeX - LaTeX StackExchange 上启动:

\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}

在此处输入图片描述

相关内容