5

I wish to use the xkeyval package to overide the 9 arguments limit and to clarifiy the different arguments (compulsory or optional). In my arguments/parameters, I have complete paragraphs possibly including \par or \\\\. I know that some macros accepts \par, others don't, to avoid hidden problematic nestings I understand. It seems that the prefix \long allows to have \par within arguments. In the implementation part of the doc, I see plenty of these \long macros.

Does that guarantee I can use \par in the arguments/value within the package xkeyval?

  • 2
    Welcome to TeX.SX! Did you try it? ;-) – user31729 May 31 '15 at 9:38
  • Yes it should work. Alternatively, if you are using aTeX and use \newcommand instead \def to define macros then this is automatic. Other differences are discussed in What is the difference between \def and \newcommand?. – Andrew May 31 '15 at 9:52
  • @ChristianHupfer I am trying ! But, since I don't know how to use xkeyval, if one says "no way, it won't work" I would try something else :) – user1771398 May 31 '15 at 10:28
  • @user1771398: Well, it works, as shown in my answer ;-) – user31729 May 31 '15 at 10:30
  • @ChristianHupfer Thanks for the pedagogic document that allow to see what works and what doesn't ! Now I feel I understand the 'xkeyval' and application to text with '\par', '\\' etc. Together with Andrew, I also better understand the nature of '\newcommand' and '\newcommand*' – user1771398 May 31 '15 at 16:02
4

\newline or \\ after an empty line would be an error anyway, but otherwise it's ok (if really necessary ;-)). \par or empty lines will do in a key as well.

Just mustn't define the command \foocmd as \newcommand*{} but as \newcommand.

If you want to be on the safe side, use \NewDocumentCommand{\foocmd}{+o+m}{...} from the xparse package, the + character allows explicitly \par etc. in the argument then.


Some background

TeX's \def command does not allow arguments with a parbreak etc, unless the \def usage is preceeded with \long (see the example below).

LaTeX's \newcommand is basically a wrapper for \long\def..., so it does allow \par, empty lines and \newline and \\, whereas \newcommand* is the 'un\long' version of it, disallowing this features.


\documentclass{article}

\usepackage{xkeyval}

\makeatletter

\define@key{foofamily}{foo}{%
  \def\KVfookey{#1}%
}




\def\foodef[#1]#2{%
\setkeys{foofamily}{#1}%

\ifdefined\KVfookey
  The key had the value \KVfookey
\fi

Mandatory argument was #2
}

\long\def\foodeflong[#1]#2{%
\setkeys{foofamily}{#1}%

\ifdefined\KVfookey
  The key had the value \KVfookey
\fi

Mandatory argument was #2
}


\makeatother


\newcommand*{\fooother}[2][foo={A long

key
}]{%
\setkeys{foofamily}{#1}%
Arg was #2%
}

\newcommand{\foocmd}[2][foo={A long

Arg 

}]{%
\setkeys{foofamily}{#1}%
The key was \KVfookey

And this is the 2nd argument: #2%
}

\begin{document}

% Won't work, since arg isn't long
%\foodef[foo={Some 

%key}]{Hello World again}

% Won't work, since defined with `\newcommand*` .. i.e not \long\def...
%\fooother{Hello World}

\begin{itemize}
\item Using a \verb+\long\def+ command -- works
\foodeflong[foo={Some 

key}]{Hello World again}

\item Using a \verb+\newcommand+ command -- works

\foocmd{Hello World}

\foocmd[foo={A very long command  \newline\newline


\par
\parskip=5cm

key}]{... which is absolutely useless}

\end{itemize}

\end{document}

enter image description here

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.