Why is there a conflict between \kern and this Separator & customized-Tooltips command

Because the MWE below uses the line

LINE 1:

\def\anyX#1{#1} %%% OK


it compiles without a hassle.

If one replaces LINE 1 with the (also included in the MWE) following line (but uncomment it):

LINE 2:

\def\anyX#1{\tooltip{#1}{something}} %%% OK (LINE 2)


then it still compiles without a hassle.

But if one chooses the following line instead of LINE 1 or LINE 2 (but uncomment it):

LINE 3:

\def\anyX#1{\tooltips[height=17cm]{#1}} %%% NOT OK (LINE 3)


one gets all kinds of errors, starting with:

: Missing number, treated as zero.

followed by a whole lot of

: Illegal unit of measure (pt inserted).

The problem is the insertion of any \kerns (e.g. \kern1.037em). Indeed, if we remove the \kern1.037em from the MWE below, we can compile the MWE again, i.e. also using LINE 3 without any problem.

However, I need to adjust the horizontal spacing for all of the punctuation marks (in the MWE we used > as an example), and therefor I need the \kerns or something similar. How could I solve this incompatibility problem please?

MWE

% !TEX TS-program = lualatexmk
%
\documentclass{scrreprt}
\usepackage{xparse}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Surpress errors
\usepackage{silence}
\ErrorFilter{latex}{File}
\WarningFilter{latex}{ignoring duplicate destination}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Separator (\maincommand)
\bgroup \catcode!=3 \catcode?=3
\gdef\replacestrings#1#2{\long\def\replacestringsA##1#1{\def\tmpb{##1}\replacestringsB}%
\expandafter\replacestringsB\fi}% improved version <May 2016> inspired
\expandafter\replacestringsA\tmpb?#1!#1% from pysyntax.tex by Petr Krajnik
\long\def\replacestringsA##1?{\def\tmpb{##1}}\expandafter\replacestringsA\tmpb
}
\egroup
%
\def\maincommand#1{\def\tmpb{#1}%
%
\replacestrings {>}  {\kern1.037em&>\kern1.037em}%
%
\expandafter\anyA\tmpb&{}%
}
\def\anyA#1&#2{\anyX{#1}#2\ifx&#2&\else\expandafter\anyB\fi}
\def\anyB{\futurelet\next\anyC}
\def\anyC{\expandafter\ifx\space\next\space\fi\anyA}
%
\def\anyX#1{#1} %%% OK (LINE 1)
% \def\anyX#1{\tooltip{#1}{something}} %%% OK (LINE 2)
% \def\anyX#1{\tooltips[height=17cm]{#1}} %%% NOT OK (LINE 3)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Customized \tooltips command
\usepackage{graphicx}
\ExplSyntaxOn
\NewDocumentCommand{\tooltips}{sO{}m+O{}}
{
\tl_map_inline:nn { #3 }
{
\IfBooleanTF{#1}{\tooltip*}{\tooltip*}{##1}{\includegraphics[#2]{files/images/##1}}#4
}
}
\ExplSyntaxOff
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Standard tooltips with LaTeX
% non-draggable version:
% for Evince (visible on click, not draggable)
%
\usepackage{pdfbase}[2017/03/16]
%\usepackage{xparse}
\usepackage{ocgbase}
\usepackage{xcolor,calc}
\usepackage{tikz}
\usetikzlibrary{calc}
%
\ExplSyntaxOn
\let\tpPdfAnnot\pbs_pdfannot:nnnn\let\tpPdfLastAnn\pbs_pdflastann:
\let\tpAppendToFields\pbs_appendtofields:n
\def\tpPdfXform{\pbs_pdfxform:nnnnn{1}{1}{}{}}
\let\tpPdfLastXform\pbs_pdflastxform:
\ExplSyntaxOff
%
\makeatletter
\NewDocumentCommand{\tooltip}{ssO{black}mO{black}m}{{%
\leavevmode%
\IfBooleanT{#1}{%
\ocgbase@new@ocg{tipOCG.\thetcnt}{%
/Print<</PrintState/OFF>>/Export<</ExportState/OFF>>%
}{false}%
\xdef\tpTipOcg{\ocgbase@last@ocg}%
}%
\IfBooleanTF{#2}{%
/Subtype/Link/Border [0 0 0]/A <</S/SetOCGState/State [/Toggle \tpTipOcg]>>
}{%
/Subtype/Screen%
\IfBooleanTF{#1}{%
/AA<<%
/E<</S/SetOCGState/State [/ON \tpTipOcg]>>%
/X<</S/SetOCGState/State [/OFF \tpTipOcg]>>%
>>%
}{
/AA<<%
/E<</S/JavaScript/JS(%
var fd=this.getField('tip.\thetcnt');%
\IfBooleanF{#1}{%
if(typeof(click\thetcnt)=='undefined'){%
var click\thetcnt=false;%
var fdor\thetcnt=fd.rect;var dragging\thetcnt=false;%
}%
}%
if(fd.display==display.hidden){%
fd.delay=true;fd.display=display.visible;fd.delay=false;%
}%
this.dirty=false;%
)>>%
/X<</S/JavaScript/JS(%
if(!click\thetcnt&&!dragging\thetcnt){fd.display=display.hidden;}%
if(!dragging\thetcnt){click\thetcnt=false;}%
this.dirty=false;%
)>>%
/U<</S/JavaScript/JS(click\thetcnt=true;this.dirty=false;)>>%
/PC<</S/JavaScript/JS (%
var fd=this.getField('tip.\thetcnt');%
try{fd.rect=fdor\thetcnt;}catch(e){}%
fd.display=display.hidden;this.dirty=false;%
)>>%
/PO<</S/JavaScript/JS(this.dirty=false;)>>%
>>%
}
}%
}{{\color{#3}#4}}%
\sbox\tiptext{\fcolorbox{black}{#5}{#6}}%
\edef\twd{\the\wd\tiptext}%
\edef\tht{\the\ht\tiptext}%
\edef\tdp{\the\dp\tiptext}%
\tpPdfXform{\tiptext}%
%tip box placed at top left page corner
\begin{tikzpicture}[remember picture,overlay]
\node [inner sep=0pt, anchor=base] at (current page.north west) {%
\raisebox{-1.5cm-\tht}[0pt][0pt]{%
\tpPdfAnnot{\twd}{\tht}{\tdp}{%
/Subtype/Widget/FT/Btn/T (tip.\thetcnt)%
/AP<</N \tpPdfLastXform>>%
/MK<</TP 1/I \tpPdfLastXform/IF<</S/A/FB true/A [0.0 0.0]>>>>%
\IfBooleanTF{#1}{%
/Ff 65537/OC \tpTipOcg%
}{%
/Ff 65536/F 3%
/AA <<%
/U <<%
/S/JavaScript/JS(%
var fd=event.target;%
var mX=this.mouseX;var mY=this.mouseY;%
var drag=function(){%
var nX=this.mouseX;var nY=this.mouseY;%
var dX=nX-mX;var dY=nY-mY;%
var fdr=fd.rect;%
fdr[0]+=dX;fdr[1]+=dY;fdr[2]+=dX;fdr[3]+=dY;%
fd.rect=fdr;mX=nX;mY=nY;%
};%
if(!dragging\thetcnt){%
dragging\thetcnt=true;Int=app.setInterval("drag()",1);%
}%
else{app.clearInterval(Int);dragging\thetcnt=false;}%
this.dirty=false;%
)%
>>%
>>%
}%
}%
\tpAppendToFields{\tpPdfLastAnn}%
}%
};
\end{tikzpicture}
\stepcounter{tcnt}%
}}
\makeatother
\newsavebox\tiptext\newcounter{tcnt}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\maincommand{A>A}

\end{document}


NOTE

Please note that the difference between LINE 2 (OK) and LINE 3 (NOT OK) is very small.

• LINE 2 uses the "Standard tooltips with LaTeX" (issued by the command \tooltip{<link text>}{<tip text>}).
• LINE 3 instead uses a "Customized \tooltips command". The definition of this command is very small as well. It goes as follows:

...(the following is just copied from the MWE) ...

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Customized \tooltips command
\usepackage{graphicx}
\ExplSyntaxOn
\NewDocumentCommand{\tooltips}{sO{}m+O{}}
{
\tl_map_inline:nn { #3 }
{
\IfBooleanTF{#1}{\tooltip*}{\tooltip*}{##1}{\includegraphics[#2]{files/images/##1}}#4
}
}
\ExplSyntaxOff
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


This customized Tooltips command is adapted from User egreg's beautiful answer to another OP entitled "How to start with a, b, c,… and end up with 1a2a3, 1b2b3, 1c2c3,".

Then, this answer also uses code referred to as "Separator (\maincommand)". This separator-code is adapted from User wipet's answer at another OP entitled "Automatically put certain inputs (e.g. punctuation marks) outside of the environment/command".

Explanation and purpose

In this dummy example code, we used:

\maincommand{A>A}


But in fact it will be used for Chinese characters (I omitted this, not to make the MWE load any further packages and fonts et cetera):

\maincommand{輕盈柔美的樣子。唐.杜牧〈 贈別〉詩二首之一:「 娉娉褭褭十三餘 ，荳蔻梢頭二月初。」}


The A above represents a Chinese character (e.g. 輕). The > represents a punctuation mark (e.g. 。).

The point of the code is to use a customized tooltip command (using \tooltips in the MWE) which will output an stroke-order-sequence-image of every Chinese characters when a user hovers over it in e.g. Adobe Acrobat Reader, so that a student of the Chinese language will be able to know the 'correct' stroke order for that character.

Now, I have to check when I have some time, but probably due to the way the whole \tooltips is defined, the kerning is messed up, especially around punctuation marks. Also, we need to have the \tooltips command to look for an image for each and every Chinese character. Therefor, we use a Separator command.

However, since the kerning around punctuation marks is messed up, we will need to change the spacing around them using the \kern (the necessary spacing there will differ among certain categories of punctuation marks ... see this OP before I revised it).

• You say that using line 2 is OK, but not for me. It reports undefined control sequence \pbs_pdfxform:nnnnn. Please, make your example more simple, especially without any expl3 code. Moreover, I cannot imagine what is your intend, specially, what do you expect when \maincommand{A>A} is used. What should be happen? I know, what happens: the \tooltips[height=17cm]{A\kern 1.037em} followed by \tooltips[height=17cm]{\kern 1.037emA} is invokend. What does it mean? – wipet Oct 12 '17 at 12:27
• Maybe your problem is that there is no picture with the name "files/images/\kern1.037emA" – wipet Oct 12 '17 at 12:40
• @wipet No, not having the pictures is not the problem. There is no picture for A either. The > is just a punctuation mark and should be interpreted as such. The tooltips shouldn't incorporate it, which is why it is "listed" as one of the separators in the separator code. – O0123 Oct 12 '17 at 12:41
• @wipet I will think about and try to answer your other questions later (probably tomorrow) as I still have some work today. My apologies. – O0123 Oct 12 '17 at 12:42
• But \includegraphics{files/images/\kern1.037emA} is called and IMHO this macro tries to find such file. – wipet Oct 12 '17 at 12:43

Your explanation gives more light to your problem. You want to write

\maincommand{ABC,DE;FG:H}


where ABCDEFGH are Chinese characters and ,;: are a special punctuations. And this example may do something like this:

\chinchar{A}\chinchar{B}\chinchar{C}\specpunct{,}\chinchar{D}\chinchar{E}
\specpunct{;}\chinchar{F}\chinchar{G}\specpunct{:}\chinchar{H}


where \chinchar can be defined for example like

\def\chinchar#1{\tooltip*{#1}{\includegraphics [height=17cm]{files/images/#1}}


and \specpunct prints given punctuation and maybe adds some kerning. If this is really your intend then you can try this:

\def\chinchar#1{[#1]} % redefine it if you want...
\def\punctdef#1{\expandafter\def\csname p-\string#1\endcsname}

\def\maincommand#1{\maincommandA#1\relax}
\def\maincommandA#1{\ifx\relax#1\else
\expandafter\ifx\csname p-\string#1\endcsname\relax \chinchar{#1}%
\else \csname p-\string#1\endcsname\fi
\expandafter\maincommandA\fi
}

\punctdef,{,\kern1em}
\punctdef:{:\kern1.5em}
\punctdef;{;\kern13pt}
\punctdef\endgraf{\endgraf}

\maincommand{ABC,DE;F\endgraf G:H}

\bye

• Finally, I am wondering how I could let the code compile using if the text between \maincommand{...} might include \\ or \endgraf. Previously, I solved this by \replacestrings {\\} {&\\} and \replacestrings {\endgraf} {&\endgraf}. How could we do something similar but with your code instead please? Simply e.g. \punctdef{\endgraf}{\endgraf} doesn't seem to work. That yields a Undefined Control Sequence. – O0123 Oct 13 '17 at 3:13
• @VincentMiaEdieVerheyen I did correct the example code in order to you can define something like \punctdef\\{\\}. See above. The \string prefix is added before #1 in \csname...\endcsname (three times in this code). – wipet Oct 13 '17 at 5:19
• I have also found some strange incompatibility when using your code (I was still using the earlier version at that time), along with either of the punctuation marks ﹑ and ～, which only arise if you include \usepackage{luatexja-fontspec}. Therefor I have opened a new OP entitled "\usepackage{luatexja-fontspec} - What's so special about ﹑and ～ (OP2)". – O0123 Oct 13 '17 at 5:28

When you are use \tooltips from LINE 3 then your \maincommand{A>A} invokes:

\tooltips[height=17cm]{A\kern 1.037em} ... which calls:
\tooltip*{A}{\includegraphics [height=17cm]{files/images/A}}
\tooltip*{\kern }{\includegraphics [height=17cm]{files/images/\kern }}
\tooltip*{1}{\includegraphics [height=17cm]{files/images/1}}
\tooltip*{.}{\includegraphics [height=17cm]{files/images/.}}
\tooltip*{0}{\includegraphics [height=17cm]{files/images/0}}
\tooltip*{3}{\includegraphics [height=17cm]{files/images/3}}
\tooltip*{7}{\includegraphics [height=17cm]{files/images/7}}
\tooltip*{e}{\includegraphics [height=17cm]{files/images/e}}
\tooltip*{m}{\includegraphics [height=17cm]{files/images/m}}
\tooltips[height=17cm]{\kern 1.037emA}  ... which calls:
\tooltip*{\kern }{\includegraphics [height=17cm]{files/images/\kern }}
\tooltip*{1}{\includegraphics [height=17cm]{files/images/1}}
\tooltip*{.}{\includegraphics [height=17cm]{files/images/.}}
\tooltip*{0}{\includegraphics [height=17cm]{files/images/0}}
\tooltip*{3}{\includegraphics [height=17cm]{files/images/3}}
\tooltip*{7}{\includegraphics [height=17cm]{files/images/7}}
\tooltip*{e}{\includegraphics [height=17cm]{files/images/e}}
\tooltip*{m}{\includegraphics [height=17cm]{files/images/m}}
\tooltip*{A}{\includegraphics [height=17cm]{files/images/A}}


TeX nor I totally does know what is your intend. Please, learn the ability to comprehensibly explain what is your intend. You are unable to tell this to TeX, so try to explain this to human at least.

• How are you so sure of this? Is it because of your understanding of the code, or did you see this in a log file please? – O0123 Oct 12 '17 at 14:24
• Set \tracingmacros to positive value or use \tracingall`. – wipet Oct 12 '17 at 18:58