
情境
在我的文件中,我將工具提示附加到單字的中間部分。若要示範此行為,請嘗試僅將滑鼠懸停V
在單字 中的字母上。AVA
這是我的命令的實現和使用\tooltip
。
%% This tooltip command
\newcommand\tooltip[2]{%
\special{pdf:bann<</Type/Annot /Subtype/Widget /FT/Btn /Ff 65536 /H/N /TU(#2)>>}%
#1%
\special{pdf:eann}%
}
%% is used like so
A\tooltip{V}{Tooltip text here.}A
%% which expands like so.
A\special{…}V\special{…}A
製作工具提示的具體細節與問題沒有直接關係,但我已將其包含在上下文中。
問題
透過散佈分隔單字的各個部分\special{}
,即A\special{}V\special{}A
導致部分之間的字偶距不再適用。^(注意:\special
並不特殊-這種行為也會發生在A{V}A
和中A{}V{}A
,除非使用 XeLaTeX 或 LuaLaTeX。²)
部分解決方案
我能夠使用以下命令恢復後兩部分的字距調整,改編自這個答案,它用來\futurelet
解決這個問題。
\newcommand\kernright[1]{\def\hltext{#1}\futurelet\hlnext\hldokern}
\def\hldokern{%
\sbox0{\mbox\hltext\mbox\hlnext}\sbox2{\hltext\hlnext}\kern\dimexpr\wd2-\wd0\relax%
}
(儘管這個問題具體涉及不同樣式之間的字距調整,但該解決方案仍然有效。另一個更通用的問題沒有令人滿意的解決方案。
以下是 的三個包裝器指令\tooltip
。
\newcommand\tooltipA[2]{#1}
\newcommand\tooltipB[2]{\tooltip{#1}{#2}}
\newcommand\tooltipC[2]{\tooltip{#1}{#2}\kernright{#1}}
下面是對比表和對應的渲染影像。
 A\tooltipX{V}{}A expands to AV kerned? VA kerned?
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ ‾‾‾‾‾‾‾‾‾‾ ‾‾‾‾‾‾‾‾‾‾
A. Normal kerned text AVA yes yes
B. Interrupted text A\special{}V\special{}A no no
C. Partial solution A\special{}V\special{}\kernright{V}A no yes
然而,我無法想出一種方法來編寫一個\tooltipD
命令來恢復 A 和 V 之間的字距調整,因為似乎沒有一個命令可以執行與\futurelet
.
問題
如何恢復 A 和 V 之間的字距調整?
我希望\kernleft
在命令正文中的某個位置插入一個附加命令\tooltipC
,以產生相當於圖像頂部的輸出。
微量元素
(工具提示應該可以在 XeLaTeX 中使用。立方)
\documentclass[varwidth=true]{standalone}
\newcommand\tooltip[2]{%
\special{pdf:bann<</Type/Annot /Subtype/Widget /FT/Btn /Ff 65536 /H/N /TU(#2)>>}%
#1%
\special{pdf:eann}%
}
\newcommand\kernright[1]{\def\hltext{#1}\futurelet\hlnext\hldokern}
\def\hldokern{%
\sbox0{\mbox\hltext\mbox\hlnext}\sbox2{\hltext\hlnext}\kern\dimexpr\wd2-\wd0\relax%
}
% A-C are from original question; D is adapted from Steven's answer
\newcommand\tooltipA[2]{#1}
\newcommand\tooltipB[2]{\tooltip{#1}{#2}}
\newcommand\tooltipC[2]{\tooltip{#1}{#2}\kernright{#1}}
\newcommand\tooltipD[2]{%
#1%
\setbox0=\hbox{#1}\kern-\wd0%
\tooltip{#1}{#2}%
\kern-\wd0#1%
}
\begin{document}
\tooltip{Foo}{Bar}
A\tooltipA{V}{Tooltip text}A
A\tooltipB{V}{Tooltip text}A
A\tooltipC{V}{Tooltip text}A
A\tooltipD{V}{Tooltip text}A
\end{document}
答案1
是否允許重擊?如果是這樣,這將恢復字距調整。我不理解工具提示程式碼,但試圖在這個 MWE 中展示我的意思。恢復正確字距調整的關鍵是\tooltip
在 上開始巨集#1
並在 上結束巨集#1
。
根據OP的警告,我已編輯在 s\relax
之後插入。\kern
我根據OP的評論重新編輯了。現在我完全理解第一個參數\tooltip
不僅僅是一個字符,而且可以想像是一個或幾個單詞,我修改了我的策略如下:唯一發生的字距調整/重打是在第一個和最後一個字母上的論點。因此,參數中間的空格和顯式連字號可用於換行。仍然有困難的一種情況是連字詞。此方法不會連字符,但我不太清楚為什麼。
以下是 OP 在他的評論中提供的文件的一個變體(在 PDFLaTeX 中編譯),用於測試各種方法 AD 的字距調整,當這些方法因即將換行而受到壓力時。目前提出的解決方案是方法 D,它捕捉緊排,但不能自動連字。
\documentclass{article}
%\usepackage{fontspec}
\usepackage{xcolor}
%\setmainfont{Minion Pro}
%\setmonofont{Consolas}
\usepackage{stringstrings}
\newcommand\tooltip[2]{%
\special{pdf:bann<</Type/Annot /Subtype/Widget /FT/Btn /Ff 65536 /H/N /TU(#2)>>}%
#1%
\special{pdf:eann}%
}
\newcommand\kernright[1]{\def\hltext{#1}\futurelet\hlnext\hldokern}
\def\hldokern{%
\sbox0{\mbox\hltext\mbox\hlnext}\sbox2{\hltext\hlnext}\kern\dimexpr\wd2-\wd0\relax%
}
\newcommand\tooltipA[2]{#1}
\newcommand\tooltipB[2]{\tooltip{#1}{#2}}
\newcommand\tooltipC[2]{\tooltip{#1}{#2}\kernright{#1}}
\newcommand\tooltipD[2]{%
\firstof#1\relax%
\tooltip{#1}{#2}%
\substring[q]{#1}{$}{$}%
\setbox0=\hbox{\thestring}%
\kern-\wd0\relax\thestring%
}
\def\firstof#1#2\relax{#1\setbox0=\hbox{#1}\kern-\wd0\relax}
\begin{document}
\setlength\parindent{0pt}
\newcommand\lotsofxs{\textcolor{gray!20}{xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xx}}
\newcommand\trial[4]{\lotsofxs{} #2\csname tooltip#1\endcsname{#3}{Tooltip text}#4\par}
\newcommand\fourtrials[4]{
{\texttt{#2|#3|#4}\hfill\textbf{#1}}\par\hrule\par
\trial{A}{#2}{#3}{#4}
\trial{B}{#2}{#3}{#4}
\trial{C}{#2}{#3}{#4}
\trial{D}{#2}{#3}{#4}
\bigskip
}
Default behavior: VAVA\tooltipD{V}{text}AVAV\vspace{1em}
%\rule{\textwidth}{2pt}\par
\fourtrials{Unhyphenatable word (only situation with overflow)}{A}{VxxxxxxxxxxV}{A}
\fourtrials{Multiple words}{A}{Vxxxxx xxxxxV}{A}
\fourtrials{Explicit hyphen}{A}{Vxxxxx\-xxxxxV}{A}
\fourtrials{Hyphenatable word}{‘}{Automatically}{.}
\fourtrials{Multiple words}{‘}{Auto matically}{.}
\end{document}
原解決方案:
在評論中與OP的對話引出了關於這個解決方案應該提出的兩點。一個是,如果 的第一個參數中有空格,則此方法將中斷\tooltip
。原因是空間有粘連,而粘連的寬度不固定。因此,字距調整不能用於撤銷空格。
第二點是單字中間的工具提示將阻止該單字的連字符。雖然這不會使該方法“損壞”,但它確實允許原本需要連字符的長單字不會這樣做,中間有一個工具提示。因此,人們可能會遇到邊距超限,不是因為該方法直接導致的,而是因為間接阻止了正常的連字。
\documentclass{article}
%% This tooltip command
\newcommand\tooltip[2]{%
#1%
\setbox0=\hbox{#1}\kern-\wd0\relax%
\special{pdf:bann<</Type/Annot /Subtype/Widget /FT/Btn /Ff 65536 /H/N /TU(#2)>>}%
\phantom{#1}%
\special{pdf:eann}%
\kern-\wd0\relax#1%
}
\parskip 1ex
\begin{document}
\noindent\rule{\textwidth}{2pt}
The tooltipped word is ``crAVAt,'' with the tooltip on the ``V''.
\noindent$\bullet$ \textbf{As it is supposed to work}
ccvcvf adsakljfd kdfklj sdkljsdf kjsdkj ds fdsfdggfds dd dgfd fsf dsf ddd
crA\tooltip{V}{Tooltip text}At
dfsf sdf s sd fsdf sdfsd sd ds sd frsdr d sfsd fds
ccvcvf adsakljfd kdfklj sdkljsdf kjsdkj ds fdsfdggfds dd dgfd fsf dsf dd %d
crA\tooltip{V}{Tooltip text}At
dfsf sdf s sd fsdf sdfsd sd ds sd frsdr d sfsd fds
\noindent$\bullet$ \textbf{Tooltip prevents hyphenation, which can cause margin overruns}
ccvcvf adsakljfd kdfklj sdkljsdf kjsdkj ds fdsfdggfds dd dgfd fsf ds
crA\tooltip{V}{Tooltip text}Atinated
the prior word is tooltipped.
ccvcvf adsakljfd kdfklj sdkljsdf kjsdkj ds fdsfdggfds dd dgfd fsf ds
crAVAtinated
the prior word is not tooltipped.
\noindent$\bullet$ \textbf{Spaces (glue) in tooltip's argument 1 can cause non-alignment of kern}
Here are spaces in the first argument iddsfxsxddgfdxffdxxfgsfdsfdd
crA\tooltip{V V}{Tooltip text}At
Here the tooltip was on the ``V V'' in the middle of ``crAV VAt.''
\end{document}
答案2
也許我只是在測量文字後明確地重新添加緊排,無論相鄰字元是否被框邊界分隔。
\documentclass[varwidth=true]{standalone}
\newcommand\tooltip[2]{%
\special{pdf:bann<</Type/Annot /Subtype/Widget /FT/Btn /Ff 65536 /H/N /TU(#2)>>}%
#1%
\special{pdf:eann}%
}
\newcommand\tooltipZ[6]{%
#1%
\kerna{#1}{#2}%
\tooltip{#3}{#4}%
\kerna{#5}{#6}%
#6%
}
\newcommand\kerna[2]{%
\sbox0{#1}\sbox2{#2}\sbox4{#1#2}%
\kern\dimexpr\wd4-\wd0-\wd2\relax}
\begin{document}
\tooltip{Foo}{Bar}
AVA
% #1 pre-char
% #2 first active char
% #3 active text
% #4 tooltip
% #5 last active char
% #6 post-char
\tooltipZ{A}{V}
{V}{Tooltip text}
{V}{A}
\end{document}
答案3
我的建議是稍微重新設計\tooltip
文法。該巨集的第一個參數是整個單詞,但突出顯示的文字被包圍在第二個大括號對中,例如:
\tooltip{A{V}Atar}{avatar}
如果沒有第二個大括號對,則整個文字將會反白顯示:
\tooltip{tipped text}{tip}
這可以簡單地實現,無需重疊字母:
\def\tooltip#1{\tooltipA#1{}\end}
\def\tooltipA#1#{\def\tmpa{#1}\tooltipB}
\def\tooltipB#1#2\end#3{\ifx\end#1\end
\tooltipC{}\tmpa{}{#3}\else \tooltipC\tmpa{#1}{#2}{#3}\fi
}
\def\tooltipC#1#2#3#4{% #1=pre-text, #2=tipped text, #3=post-text, #4=tip
#1\kerncorr{#1}{#2}%
\special{pdf:bann<</Type/Annot /Subtype/Widget /FT/Btn /Ff 65536 /H/N /TU(#4)>>}%
#2%
\special{pdf:eann}%
\kerncorr{#2}{#3}#3%
}
\def\kerncorr#1#2{\setbox0=\hbox{#1\kern0pt #2}\setbox2=\hbox{#1#2}%
\kern\wd2 \kern-\wd0
}
如果需要的話可以直接設定連字符\-
,例如
\tooltip{A{V}A\-tar}{avatar}