I am very excited about this code by User @egreg which I will be using to display images of the stroke order of a Chinese character whenever you hover over a certain character.
I changed the code a little bit for my use-case, to end-up with something as such (note I used the font HanWangKaiMediumChuIn_wp010-08.ttf, so one needs to download it in case one wishes to use the font):
\documentclass[a6paper,12pt]{scrbook}
\usepackage{fontspec}
\setmainfont{HanWangKaiMediumChuIn_wp010-08}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% tooltips with LaTeX
%
% optimized for Adobe Reader (visible on mouse-over)
% usage: \tooltip[<link colour>]{<link text>}[<tip box colour>]{<tip text>}
% non-draggable version:
% usage: \tooltip*[<link colour>]{<link text>}[<tip box colour>]{<tip text>}
%
% for Evince (visible on click, not draggable)
% usage: \tooltip**[<link colour>]{<link text>}[<tip box colour>]{<tip text>}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{pdfbase}[2017/03/16]
\usepackage{xparse,ocgbase}
\usepackage{xcolor,calc}
\usepackage{tikz}
\usetikzlibrary{calc}
\ExplSyntaxOn
\let\tpPdfLink\pbs_pdflink:nn
\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{blue}mO{yellow!20}m}{{%
\leavevmode%
\IfBooleanT{#1}{%
\ocgbase@new@ocg{tipOCG.\thetcnt}{%
/Print<</PrintState/OFF>>/Export<</ExportState/OFF>>%
}{false}%
\xdef\tpTipOcg{\ocgbase@last@ocg}%
}%
\tpPdfLink{%
\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{-\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}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ExplSyntaxOn
\NewDocumentCommand{\tooltips}{sO{}m+O{}}
{
\tl_map_inline:nn { #3 }
{
\IfBooleanTF{#1}{\tooltip*}{\tooltip*}{##1}{\includegraphics[#2]{images/##1}}#4
}
}
\ExplSyntaxOff
\begin{document}
\tooltips[scale=1]{高一生}
\end{document}
Another note: be sure to compile twice.
The code however, is designed to look for an image of the same name as the character. For example, if one would hover over the character 高, the code currently looks for the image 高.png (in the folder images).
Now, this is designed to work with a large set of Chinese characters. In fact, I have the required images of thousands of such characters. Unfortunately, the images are not named e.g. 高.png, not even U+9AD8.png (the UCS of that Character), but it is now named B0AA.png, which is the Big-5 encoding of that character.
So, that leaves me with two options to make this code workable.
- Either, I have to batch convert all of the names of each and every
.pnginto their respective Chinese characters (for exampleB0AA.pnginto高.png). But I wouldn't know how to do this. Is there a way to do this with Tex? - Alternatively. How could I adapt the code above so that the code will sort of "convert" each character in
\tooltips[scale=1]{高一生}into the Big-5 encoding but only when looking for the respective image of each character (so that\tooltips[scale=1]{高一生}will still display高一生but look for the imagesB0AA.png,A440.pngandA5CD.pngrespectively).
Anybody offering a solution please?
Please note that I do have a .txt file available which might be used as a conversion table, as it lists all the necessary characters as such:
UCS Big5
UCS Big5
UCS Big5
... ...
As for the purpose of a M(not yet-)WE example, one could use the following minimal .txt file, which contains the UCS (left) and Big5 (right) codes for 高一生:
U+9AD8 B0AA
U+4E00 A440
U+751F A5CD
By the way, if you want to use the images I am using, you can find them at
http://stroke-order.learningweb.moe.edu.tw/words/B0AA.png
at http://stroke-order.learningweb.moe.edu.tw/words/A440.png
and at http://stroke-order.learningweb.moe.edu.tw/words/A5CD.png
That is, for the example case of 高一生. Be sure to put them in a folder called "images" next to your main .tex file.
Finally, this code is supposed to produce .pdf files to be viewed with Adobe Acrobat Reader. I don't need to be bound by a specific compiling method, but the only way I have been successfully able to compile this, is using LuaLaTeX. As per this answer by @David Carlisle, XeTeX might be an other option, but I can't figure out what minute changes I would have to make.

lualatex(I haven't tried), then it may be easier to write this encoding conversion as a piece of Lua code. Of course in principle it can be done in TeX macros too, and as you already have a file for use as a conversion table, using that data may make the problem easier. – ShreevatsaR Sep 20 '17 at 02:08LuaLaTeX, let me just mention that in the OP. Would you have a possible solution? – O0123 Sep 20 '17 at 02:10.txtfile contain the UCS <-> Big5 mapping for every character that you care about? Can you upload your.txtfile somewhere and add the link here? – ShreevatsaR Sep 20 '17 at 02:45高一生) to be saved as a .txt file in the OP (first line:U+9AD8 B0AA, second line:U+4E00 A440, third line:U+751F A5CD) – O0123 Sep 20 '17 at 02:55It won't work. You can't ping people from postsas it is not directly related to the main question, please? – O0123 Sep 20 '17 at 05:37