5

UPDATED, see edit history for completeness

I'm trying out the keycommand package but it's not working as expected. There seems to be some incompatibility with tables, but I've read and re-read the documentation and I can't figure out what it is.

I've built a minimal example of what I'm doing. It's using longtable, since those seem to work a little better than regular ones and since that is what I need it to work with (for now):

\documentclass{article}
\usepackage{longtable} 
\usepackage{keycommand}

\newkeycommand\mycommand[first=one,second=2][1]{%
#1 \\    
First: \commandkey{first} & second: \commandkey{second}%
}

\begin{document}
\begin{longtable}{ll}
\mycommand{hello} \\
\mycommand[first=1,second=two]{world!}
\end{longtable}
\end{document}

The problem is that instead of outputting the parameter values, it outputs the parameter names:

EXPECTED OUTPUT                      ACTUAL OUTPUT
***************                      *************
hello                                hello
First: one    second: 2              first: first        second: second
world!                               world!
First: 1      second: two            first: first        second: second

Why does it do this? What can I do about it?

6
  • Sorry, but you should change your MWE, because it clearly doesn't reflect the real problem, which most likely has to do with the fact that you're trying to use the command in a table.
    – egreg
    Jan 15, 2013 at 22:59
  • You're probably right - when I tried wrapping the commands in a table and adding some &s and \\ s, it didn't even compile anymore... I'll keep trying some stuff and update if I find anything I think is useful. Thanks for the hint! Jan 15, 2013 at 23:06
  • @egreg: You were absolutely right - it stopped working when in a table. For a regular table it just didn't compile (complaining about too many &s) but with longtable (which is what I need) the MWE showed exactly the behavior I observe in my actual code. Would you mind taking a new look at this? Jan 15, 2013 at 23:20
  • Well, you've discovered that \newkeycommand defined macros don't work across table columns. :( There are problems in timing: when you do \\, the local assignments performed at the call of \mycommand are lost.
    – egreg
    Jan 15, 2013 at 23:32
  • After looking at how the package works (for what I've been able to grasp) I don't think there's much to do: the assignments performed to provide a value when you call \commandkey are local; the \\ in the definition ends the group where these assignments are made and keycommands loses track of what the values should be, so it prints the key name.
    – egreg
    Jan 16, 2013 at 0:06

2 Answers 2

1

This seems to be a bad bug in keycommands. The assignments performed to provide a value when you call \commandkey are local; the \\ in the definition ends the group where these assignments are made and keycommands loses track of what the values should be, so it prints the key name.

You can use a different (and reliable) method for obtaining the same result.

\documentclass{article}
\usepackage{longtable} 
\usepackage{xparse}

\ExplSyntaxOn
% Set up the keys
\keys_define:nn { lycken-mycommand }
 {
  first  .tl_gset:N = \g_lycken_first_tl,
  second .tl_gset:N = \g_lycken_second_tl,
 }

\NewDocumentCommand{\mycommand}{ O{} m }
 {
  \keys_set:nn { lycken-mycommand }
   {
    % provide the default values
    first = first,
    second = 2,
    #1 % evaluate the keys in the optional argument
   }
  #2 \\    
  First:~\g_lycken_first_tl & second:~\g_lycken_second_tl
 }
\ExplSyntaxOff

\begin{document}
\begin{longtable}{ll}
\mycommand{hello} \\
\mycommand[first=1,second=two]{world!}
\end{longtable}
\end{document}

enter image description here

2
  • I've finally gotten around to trying this out, and it seems this doesn't work in paragraph columns. When I try to embed the options in longer paragrahps of text, all the text is still on one line, with all the whitespace between words gone, and the line is far too long to fit on in the cell, but extends instead out to the right, and off the page... MWE: pastebin.com/EjxRtJid Apr 17, 2013 at 18:22
  • @TomasLycken In \ExplSyntaxOn mode, the spaces are ignored; if you want to set a space you have to use ~. The command \@valuta is not defined and should be used in a \makeatletter context (which is not ensured by \ExplSyntaxOn or expressed as \use:c{@valuta}.
    – egreg
    Apr 17, 2013 at 19:55
1

This is trivial and possible without any global assignments (any assignments for that matter) using expkv-cs (the following uses its Hash variant, as that is closer in syntax to keycommand, even though the example only needs two keys and it would be faster to set it up with the Split variant).

Disclaimer: I'm the author of expkv

\documentclass{article}
\usepackage{longtable} 

\usepackage{expkv-cs}
% grabbing the argument as an optional argument
\newcommand\mycommand[1][]{\mycommandKV{#1}}
% parsing the keys "first" and "second"
\ekvcHashAndForward\mycommandKV\mycommandOUT{first=one,second=2}
% output
% #1 contains the parsed keys accessible via \ekvcValue
% #2 the mandatory argument
\newcommand\mycommandOUT[2]
  {%
    #2 \\
    First: \ekvcValue{first}{#1} & second: \ekvcValue{second}{#1}%
  }

\begin{document}
\begin{longtable}{ll}
\mycommand{hello} \\
\mycommand[first=1,second=two]{world!}
\end{longtable}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .