3

Error while using the new command \reponse created in the preambule.

What is wrong with the definition of the newcommand ? It must be linked with comment package ...

Example that works seems the same : How to use `\begin` & `\end` in a new command

Thanks for help.

Error : File ended while scanning use of \next \end{answer} while using the commande \reponse

\documentclass{article}
\usepackage{comment}
\includecomment{answer}
\newcommand{\reponse}[1]{%
\begin{answer}
{\textbf{The answer is :}#1}
\end{answer}}


\begin{document}
% i would like to write :
\reponse{ok}
\reponse{ok}
% ...
% instead of :
\begin{answer}
{\textbf{The answer is:} ok}
\end{answer}                       <- error line 
\begin{answer}
{\textbf{The answer is:} ok}
\end{answer}
%...

\end{document}
  • 2
    You can't use “comment environments” as arguments to commands, just like you can't with verbatim: a comment environment is treated the same. – egreg Jan 24 '17 at 13:19
2

Like @egreg said, you can not use comment environment as arguments. The difficulty comes from a special condition given in the package documentation: The opening and closing commands should appear on a line of their own. No starting spaces, nothing after it.

But here is what I can suggest (The trick comes from section 2 Usage of the comment documentation):

\documentclass[english]{article}
\usepackage{comment}
\usepackage{babel}
\includecomment{answer}
\makeatletter 
\newcommand{\reponse}[1]{\@bsphack\@esphack}
\makeatother 
\begin{answer}
\renewcommand{\reponse}[1]{\textbf{The answer is :}#1}
\end{answer}


\begin{document}
% i would like to write :
\reponse{ok}
\reponse{ok}
% ...
% instead of :
\begin{answer}
{\textbf{The answer is:} ok}
\end{answer}

\begin{answer}
{\textbf{The answer is:} ok}
\end{answer}
%...
\end{document}

EDIT Taking into account UlrichDiez's comment.

  • Nice logic to create an empty new command, and optionaly (if answer is set) modify it with a renew command ! – Patrick Dezecache Jan 24 '17 at 13:41
  • 2
    In case \response doesn't print anything - shouldn't it preserve the space-factor? -- I.e., \makeatletter \newcommand{\reponse}[1]{\@bsphack\@esphack}\makeatother \begin{answer}\renewcommand{\reponse}[1]{\textbf{The answer is:} #1}\end{answer} (By the way: I think the blank space should be behind the colon, not before it.) – Ulrich Diez Jan 24 '17 at 18:54
  • You're right @UlrichDiez. Even if I do not think the code will be used for in-line comments, it looks better taking into account your comment. – Jérôme Dequeker Jan 25 '17 at 6:39

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.