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

如果這改變了任何事情,我正在使用背頁。我也可以對與錯誤無關的程式碼進行任何最佳化。謝謝。

編輯:將值更改為阿拉伯語,這消除了 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鏈來觸發擴展,直到您獲得值,而進一步,您需要四個\expandafter鏈:

 \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獲取的第一個鏈。\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}

在此輸入影像描述

您可能對\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}

在此輸入影像描述

相關內容