1

I am trying to define a new command to customize the item-based behavior within the LaTeX, as below:

\newcommand{\myitemize}[2]{
\begin{description}
\item[$\{#1}$]{#2}
\end{description}
}

It is supposed to be called as:

\myitemize{bullet}{matinking}

But there is an error, as below, which does not let the document to be compiled:

! LaTeX Error: Missing \begin{document}.

The problem is seemingly with the utilization of the \begin & \end within the command.

Would you please guide me how to handle this case?

Thanks in advance...

PS. I've declared the new command before the \begin{document}.

  • Don't reinvent the wheel: use enumitem. – cfr Oct 9 '15 at 13:10
  • 1
    I think the problem is not related to what you say, but is due to the non-matching \{ and } (the first typesets a {, while the second closes a group, which had not been opened). – LucaD Oct 9 '15 at 13:11
  • @cfr: Thanks for the suggestion. But I am eager to realize my fault within above definition, too... – Roboticist Oct 9 '15 at 13:11
  • In that case, see @LucaD 's comment. – cfr Oct 9 '15 at 13:12
  • I have to use \ to apply the command effect... such as \bullet... right? – Roboticist Oct 9 '15 at 13:13
4

The problem with your code is that you match \{ with } in your command definition. { and } are used to create a group, while \{ and \} could be used to typeset a couple of braces. The error message is given because you close a group which has not been opened.

Indeed the following code works and prints the first argument between braces. However, looking at your example use, it probably doesn't do what you intended:

\documentclass{article}
\newcommand{\myitemize}[2]{
\begin{description}
\item[$\{#1\}$]{#2}
\end{description}
}
\begin{document}
\myitemize{bullet}{matinking}
\end{document}

I guess that what you are trying to achieve is to turn the first argument of your command into a macro: this can be done using the \csnames \endcsname tex primitives, as already mentioned by cfr

\documentclass{article}
\newcommand{\myitemize}[2]{
\begin{description}
\item[$\csname #1\endcsname$]{#2}
\end{description}
}
\begin{document}
\myitemize{bullet}{matinking}
\end{document}
  • Such an omni-aspect explanation. Thanks... Would you please tell me the difference between the two cases you illustrated?... Which one might be preferable?... – Roboticist Oct 9 '15 at 13:33
  • 1
    The first is only a valid code similar to what you posted, but it produces {bullet}, which is not what you wanted, I susupect. The second turns the text bullet into the \bullet macro. – LucaD Oct 9 '15 at 13:40
2

LucaD explained the problem. To do what you want, you need to use something like \csname... \endcsname here:

\documentclass{article}
\newcommand{\myitemize}[2]{%
\begin{description}
\item[$\csname #1\endcsname$]{#2}
\end{description}%
}
\begin{document}
\myitemize{bullet}{matinking}
\end{document}

with command name

  • I would appreciate... – Roboticist Oct 9 '15 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.