2

Here is my code:

\newcommand{\testtest}[1]{%%
  \stepcounter{aebir@prob@cnt}%%
  \edef\aebir@tmp{#1}}

\testtest{$\frac{1}{2}$} \aebir@tmp{}

This prints 1/2 which is fine. But if I try to write something like

\newcommand{\testtest}[1]{%%
  \stepcounter{aebir@prob@cnt}%%
  \edef\aebir@tmp{#1}}

\testtest{\textbf{A}} \aebir@tmp{}

this not printing bold letter A. This just fails to run. This is a part of my longer code but I need to solve this problem first.

Probably the problem is due to that {#1} thing. Because

\def\aeiki@tmp{\textbf{A}}
\aeiki@tmp{}

This is working perfectly.

Thanks!


Sorry for the bad explanation. Here is my full code, this is something that I need to use for creating an answer key.

\documentclass{article}

\begin{document}

\makeatletter

\newcounter{aebir@prob@cnt} \let\aebir@answer@key\relax

\newcommand\dogrucevapbir[1]{%% \stepcounter{aebir@prob@cnt}%% \edef\aebir@tmp{\theaebir@prob@cnt/#1}%% \ifx\relax\aebir@answer@key \edef\aebir@answer@key{\aebir@tmp}%% \else \edef\aebir@answer@key{\aebir@answer@key,\aebir@tmp}%% \fi }

Question 1: Correct answer is A. \dogrucevapbir{A}

Question 2: Correct answer is B. \dogrucevapbir{B}

Question 3: Correct answer is C. \dogrucevapbir{C}

Here is the answer key:

\aebir@answer@key{}

\end{document}

Result:

This is result that I get:

But If I try something like \dogrucevapbir{\textbf{A}} or \dogrucevapbir{\dfrac{1}{2}}, I'm getting error.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036

3 Answers3

9

You can not use \edef on arbitrary latex input. You give no indication what you want the command to do, but it looks like expansion is not needed here, so you can use \def.

enter image description here

\documentclass{article}

\begin{document}

\makeatletter \newcounter{aebir@prob@cnt} \newcommand{\testtest}[1]{%% \stepcounter{aebir@prob@cnt}%% \def\aebir@tmp{#1}}

\testtest{$\frac{1}{2}$} \aebir@tmp{}

\testtest{\textbf{A}} \aebir@tmp{}

\end{document}

David Carlisle
  • 757,742
6

I'd recommend using old style commands. You can use \edef but you have to know what do you want to expand and what not.

\newcount\probcnt
\def\answerkey{}

\def\dogrucevapbir#1{% \global\advance\probcnt by1 \xdef\answerkey{\ifnum\probcnt=1 1\else \unexpanded\expandafter{\answerkey}, \the\probcnt\fi /\unexpanded{#1}}% }

Question 1: Correct answer is A. \dogrucevapbir{A}

Question 2: Correct answer is B. \dogrucevapbir{B}

Question 3: Correct answer is C. \dogrucevapbir{C}

Here is the answer key:

\answerkey

wipet
  • 74,238
  • This is a straight copy of my answer, were it not for the fact that the list you build is “static”, cannot be used for other purposes without defining an ad hoc parser and it's built with very awkward looking commands. – egreg Feb 02 '23 at 18:08
  • 6
    This is not copy of egreg's answer. I don't use \int_gincr:N, \exp_not:n etc. because I don't understand this. I use only well known TeX primitives \xdef, \the etc. My answer is usable in TeX environments where Expl3 language isn't present. – wipet Feb 02 '23 at 19:15
  • I'd say that the very awkward looking commands are in egreg's answer. This is using only well known commands as wipet says. – User Mar 05 '23 at 11:12
2

I'd recommend using new style commands.

\documentclass{article}

\ExplSyntaxOn

\int_new:N \g_ertan_answer_count_int \clist_new:N \g_ertan_answer_keys_clist

\NewDocumentCommand{\dogrucevapbir}{m} { \int_gincr:N \g_ertan_answer_count_int \clist_gput_right:Nx \g_ertan_answer_keys_clist { \int_to_arabic:n { \g_ertan_answer_count_int } / \exp_not:n {#1} } } \NewDocumentCommand{\printanswerkeys}{} { \clist_use:Nn \g_ertan_answer_keys_clist { ,~ } }

\ExplSyntaxOff

\begin{document}

Question 1: Correct answer is A. \dogrucevapbir{A}

Question 2: Correct answer is B. \dogrucevapbir{$\frac{1}{2}$}

Question 3: Correct answer is C. \dogrucevapbir{\textbf{C}}

Here is the answer key:

\printanswerkeys

\end{document}

enter image description here

egreg
  • 1,121,712