LaTeX forum ⇒ GeneralIssues when using \put in a command definition

LaTeX specific issues not fitting into one of the other forums of this category.
migf
Posts: 11
Joined: Mon Feb 18, 2013 2:33 pm

Issues when using \put in a command definition

Postby migf » Wed Apr 05, 2017 8:10 pm

I need a command that uses \put to add a hyperlink to a picture and defined it like this
  1. \newcommand{\myput}[3]{\put(#1){\makebox(0,0){\hyperlink{#2}{#3}}}}


This command is used in the context of a \multido loop from the package of same name, e.g.
  1. \myput{550,300}{lnkB.2}{$\rightarrow $}

but this raises the error
  1. ! Illegal unit of measure (pt inserted).


Is there any error in my definition or is there some limitation on the way \put can be used?

Thanks in advance for any help.

User avatar
Stefan Kottwitz
Site Admin
Posts: 8373
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Thu Apr 06, 2017 8:53 am

Can you provide some code context? Can you provide a Infominimal working example? (Nobody can test these two lines without a small document, and one has to guess which packages to use.)

Stefan
Site admin

migf
Posts: 11
Joined: Mon Feb 18, 2013 2:33 pm

Postby migf » Thu Apr 06, 2017 11:16 am

Sorry, I did not add a MWE because I thought this would be some basic problem in the command definition.

In fact it is not: to reproduce the error I had to use the multido package and also the listofitems one.

  1. \documentclass{article}
  2.  
  3. \usepackage{a4}
  4. \usepackage{multido}
  5. \usepackage{listofitems}
  6.  
  7. \newcommand{\myput}[3]{\put(#1){\makebox(0,0){\hyperlink{#2}{#3}}}}
  8. \newcommand{\hyperlink}[2]{#2 to #1}% the original document uses the hyperref package
  9.  
  10. \newcommand{\addlink}[1]{
  11. \myput{#1}{p.2}{$\rightarrow $}
  12. }
  13.  
  14. \newcommand{\addlinks}{% this works as expected
  15. \multido{\ix=3+6}{2}{%
  16. \multido{\iy=3+5}{2}{\addlink{\ix,\iy}}%
  17. }%
  18. }
  19.  
  20. \newcommand{\addlist}[1]{% the argument has the form x1,y1;x2,y2;...
  21. \setsepchar{;,}\readlist\lOfItems{#1}% creates a list of lists ((x1 y1) (x2 y2) ...)
  22. \multido{\ik=1+1}{\lOfItemslen}{% iterates on the top level list
  23. \addlink{\lOfItems[\ik,1],\lOfItems[\ik,2]}% uses coordinates x,y
  24. }%
  25. }
  26.  
  27. \begin{document}
  28.  
  29. \setlength{\unitlength}{1cm}
  30.  
  31. \begin{center}
  32. \begin{picture}(18,10)
  33. \addlink{6,6} \addlinks % ok
  34. \addlist{1,2;7,4}% list of x,y coordinates
  35. \end{picture}
  36. \end{center}
  37.  
  38. \end{document}


The error message is
  1. ! Illegal unit of measure (pt inserted).
  2. <to be read again>
  3. ]
  4. l.34 \addlist{1,2;7,4}
  5. % list of x,y coordinates


It seems there are problems with the listofitems package. A simpler version of this MWE gave another error: if \addlist is defined by
  1. \newcommand{\addlist}[1]{%
  2. \setsepchar{;}\readlist\lOfItems{#1}%
  3. \multido{\ik=1+1}{\lOfItemslen}{%
  4. \addlink{\lOfItems[\ik]}
  5. }%
  6. }

i.e., by taking the coordinates list as a flat list (x1,y1 x2,y2 ...), there is the following error
  1. Runaway argument?
  2. \lOfItems [\ik ] ){\makebox (0,0){\hyperlink {p.2}{$\rightarrow \ETC.
  3. ! File endedRunaway argument?

that I could not get rid of...

migf
Posts: 11
Joined: Mon Feb 18, 2013 2:33 pm

Postby migf » Thu Apr 06, 2017 1:24 pm

Correction to the MWE above

A slash is missing in the argument of \setsepchars in the first definition of \addlist. The correct definition is

  1. \newcommand{\addlist}[1]{%
  2. \setsepchar{;/,}\readlist\lOfItems{#1}%
  3. \multido{\ik=1+1}{\lOfItemslen}{%
  4. \addlink{\lOfItems[\ik,1],\lOfItems[\ik,2]}
  5. }%
  6. }

migf
Posts: 11
Joined: Mon Feb 18, 2013 2:33 pm

Postby migf » Thu Apr 06, 2017 2:42 pm

I have found the problem: the macros created by the listofitems package to index list elements cannot be directly used as arguments to the \addlink command in the MWE. Instead of them its \itemtomacro command must be used to obtain fully expandable macros for the elements.

  1. \newcommand{\addlist}[1]{%
  2. \setsepchar{;/,}\readlist\lOfItems{#1}%
  3. \multido{\ik=1+1}{\lOfItemslen}{%
  4. \itemtomacro\lOfItems[\ik,1]\xcoord%
  5. \itemtomacro\lOfItems[\ik,2]\ycoord%
  6. \addlink{\xcoord,\ycoord}
  7. }%
  8. }


Hope this can be of help to others.

User avatar
Stefan Kottwitz
Site Admin
Posts: 8373
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Thu Apr 06, 2017 4:06 pm

Great to see that it's solved! Thanks for posting the solution.

At the beginning, just seeing \put and \hyperlink with a box wasn't clear, that's why I asked for a MWE. I tested it, the problem was not easy to see, so good that you found it.

Stefan
Site admin


Return to “General”

Who is online

Users browsing this forum: No registered users and 8 guests