GeneralIssues when using \put in a command definition

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

Issues when using \put in a command definition

Post by migf »

I need a command that uses \put to add a hyperlink to a picture and defined it like this

Code: Select all

\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.

Code: Select all

\myput{550,300}{lnkB.2}{$\rightarrow $}
but this raises the error

Code: Select all

! 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.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

Issues when using \put in a command definition

Post by Stefan Kottwitz »

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
LaTeX.org admin
migf
Posts: 20
Joined: Mon Feb 18, 2013 2:33 pm

Issues when using \put in a command definition

Post by migf »

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.

Code: Select all

\documentclass{article}

\usepackage{a4}
\usepackage{multido}
\usepackage{listofitems}

\newcommand{\myput}[3]{\put(#1){\makebox(0,0){\hyperlink{#2}{#3}}}}
\newcommand{\hyperlink}[2]{#2 to #1}%  the original document uses the hyperref package

\newcommand{\addlink}[1]{
 \myput{#1}{p.2}{$\rightarrow $}
}

\newcommand{\addlinks}{%  this works as expected
\multido{\ix=3+6}{2}{%
 \multido{\iy=3+5}{2}{\addlink{\ix,\iy}}%
}%
}

\newcommand{\addlist}[1]{%               the argument has the form  x1,y1;x2,y2;...
\setsepchar{;,}\readlist\lOfItems{#1}%  creates a list of lists ((x1 y1) (x2 y2) ...)
\multido{\ik=1+1}{\lOfItemslen}{%        iterates on the top level list
 \addlink{\lOfItems[\ik,1],\lOfItems[\ik,2]}%  uses coordinates x,y
}%
}

\begin{document}

\setlength{\unitlength}{1cm}

\begin{center}
\begin{picture}(18,10)
  \addlink{6,6}  \addlinks % ok
  \addlist{1,2;7,4}% list of x,y coordinates
\end{picture}  
\end{center}

\end{document}
The error message is

Code: Select all

! Illegal unit of measure (pt inserted).
<to be read again> 
                   ]
l.34   \addlist{1,2;7,4}
                        % 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

Code: Select all

\newcommand{\addlist}[1]{%
\setsepchar{;}\readlist\lOfItems{#1}%
\multido{\ik=1+1}{\lOfItemslen}{%
 \addlink{\lOfItems[\ik]}
}%
}
i.e., by taking the coordinates list as a flat list (x1,y1 x2,y2 ...), there is the following error

Code: Select all

Runaway argument?
\lOfItems [\ik ] ){\makebox (0,0){\hyperlink {p.2}{$\rightarrow \ETC.
! File endedRunaway argument?
that I could not get rid of...
migf
Posts: 20
Joined: Mon Feb 18, 2013 2:33 pm

Issues when using \put in a command definition

Post by migf »

Correction to the MWE above

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

Code: Select all

\newcommand{\addlist}[1]{%
\setsepchar{;/,}\readlist\lOfItems{#1}%
\multido{\ik=1+1}{\lOfItemslen}{%
 \addlink{\lOfItems[\ik,1],\lOfItems[\ik,2]}
}%
}
migf
Posts: 20
Joined: Mon Feb 18, 2013 2:33 pm

Issues when using \put in a command definition

Post by migf »

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.

Code: Select all

\newcommand{\addlist}[1]{%
\setsepchar{;/,}\readlist\lOfItems{#1}%
\multido{\ik=1+1}{\lOfItemslen}{%
 \itemtomacro\lOfItems[\ik,1]\xcoord%
 \itemtomacro\lOfItems[\ik,2]\ycoord%
 \addlink{\xcoord,\ycoord}
}%
}
Hope this can be of help to others.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

Issues when using \put in a command definition

Post by Stefan Kottwitz »

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
LaTeX.org admin
Post Reply