Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

\pdfmarkupcomment 不支持中文 #14

Closed
taroxd opened this issue Mar 12, 2019 · 6 comments
Closed

\pdfmarkupcomment 不支持中文 #14

taroxd opened this issue Mar 12, 2019 · 6 comments

Comments

@taroxd
Copy link

taroxd commented Mar 12, 2019

一个例子:

% !TeX program = LuaLaTeX
% !TeX encoding = UTF-8

\documentclass{article}

\pagestyle{empty}

\usepackage{luatexja-fontspec}
\usepackage{luatexja-ruby}
\usepackage[rgb]{xcolor}
\usepackage[author={taroxd}]{pdfcomment}

\begin{document}

\section{标题}

\pdfmarkupcomment[markup=Squiggly,color=red]{中文}{中文}

\end{document}

使用 lualatex 编译,会得到如下错误。

Package soul Error: Reconstruction failed. [...t[markup=Squiggly,color=red]{中文}{中文}]

类似的,用 xelatex 也是得到同样的错误。这段代码将“中文”两次改为英文不会有问题。

@Liam0205
Copy link
Contributor

Liam0205 commented Mar 12, 2019 via email

@taroxd
Copy link
Author

taroxd commented Mar 12, 2019

因为有人跟我说中文的问题最好在这儿问 _(:з)∠)_

@taroxd taroxd closed this as completed Mar 12, 2019
@Liam0205 Liam0205 transferred this issue from CTeX-org/ctex-kit Mar 12, 2019
@Liam0205 Liam0205 reopened this Mar 12, 2019
@Liam0205
Copy link
Contributor

Issue reopened after transfering.

@Liam0205
Copy link
Contributor

这个应该不意外,pdfcomment 包画那些好看的样式是用 soul 包来做的。而 soul 包蛮脆弱的。

@muzimuzhi
Copy link
Collaborator

一个比较手动的 workaround

\documentclass{article}
\usepackage{ctex}
\usepackage[author={taroxd}]{pdfcomment}
\usepackage[rgb]{xcolor}
\usepackage{xpatch}

\makeatletter
\AtBeginDocument{
  \patchcmd{\SOUL@doword}{\SOUL@tt}{}{}{\ddt}
  \patchcmd{\SOUL@dosyllable}{\SOUL@tt}{}{}{\ddt}
}
\makeatother

\newfontfamily\cnFont{SourceHanSerif-Regular}

\newcommand{\pdfmarkupcommentX}[3][]{%
  \xeCJKsetup{xeCJKactive=false}%
  \cnFont
  \pdfmarkupcomment[#1]{#2}{#3}%
  \xeCJKsetup{xeCJKactive=true}
}

\begin{document}
\pdfmarkupcommentX[markup=Squiggly,color=red]{中文测试}{中文测试}
\end{document}

image
\cnFont 设置为 pdf comment 插入位置的默认中文字体,效果上就比较看不出了。如果能根据上下文实时切换合适的中文字体,会更好。

以下是尝试的过程,过程并不具有严谨的科学性,只是「凑巧」可用。


  1. pdfcomment 包调用了 soulpos 包,后者又调用了 soul 包。
  2. @Liam0205 所说,这个问题是 soul 包引起的,以下的简单例子就能复现。
\documentclass{article}
\usepackage{ctex}
\usepackage{soul}
\begin{document}
\ul{中文}
\end{document}

对应的错误是

! Package soul Error: Reconstruction failed.
  1. soul 宏包文档 Sec. 2.3.1,上述错误有两个可能来源:

    1. Grouping hyphenatable material
    2. Leaking font switches

      A hidden font switching command that leaks into its neighborship causes a ‘Reconstruction failed’ error.
      猜测问题出自第二个来源。
  2. soul 宏包提供了两种绕开 leaking font switches 的方式,简单测试后都没有生效。转而采取「关闭 xecjk 机制的方式」,发现不会报错了,但在测量字符宽度方面仍有问题。

\font\cnFont="[SourceHanSerif.ttc]"
\xeCJKsetup{xeCJKactive=false}\cnFont
this works: \ul{中文x}, \\
this does not work: \ul{中文}
  1. 发现 soul 宏包内部,在测量「待划线内容」的宽度时,会使用字体切换命令 \SOUL@tt(默认为字体 ectt1000)。尝试强行去掉这个命令,发现可行。

@muzimuzhi
Copy link
Collaborator

muzimuzhi commented Apr 4, 2019

以下是一个让 \pdfmarkupcomment 命令不依赖 soul,从而直接支持中文的尝试。

  • 通过了文本模式下 xelatex 和 lualatex 的测试,数学模式还没测。无法处理跨行的 annotation
  • pdfcomment 宏包的选项众多,懒得充分测试,如发现问题欢迎(开新 issue)反馈。
  • 这个尝试有很大的偶然因素,目前认为 \pdfmarkupcomment 可以不依赖 soulpos
  • 以下的尝试,会让 \pdfmarkupcomment 定义中的部分代码成为无用的废代码,暂不理会
\documentclass{article}
\usepackage{ctex}
\usepackage[rgb]{xcolor}
\usepackage{pdfcomment}
\usepackage{xpatch}


\makeatletter
% change the dimension that stores the width of annotation
\xpatchcmd\pc@soul@markup{\ulwidth}{\pc@box@width}{}{\ddt}

% change entrance to \@new@soul, and substitute each of 2 occurrences
\xpatchcmd\pdfmarkupcomment{\SOUL@}{\@new@soul}{}{\ddt} 
\xpatchcmd\pdfmarkupcomment{\SOUL@}{\@new@soul}{}{\ddt}

\def\@new@soul#1{%
  % \pc@box@width is provided by "pdfcomment.sty"
  \settowidth{\pc@box@width}{#1}%
  \pc@soul@markup
  #1%
}
\makeatother

\begin{document}
text

\pdfmarkupcomment[markup=Squiggly,color=red]{text}{something}

\pdfmarkupcomment[markup=Squiggly,color=red]{中文text中文:aha}{something}
\end{document}

image


目前知道的信息

% in pdfcomment.sty
\pdfmarkupcomment, def of
 - handle math mode, and "pdfcomment" options like "final" and "disable"
 - change internal entrance of \SOUL@
   % \def\ulp@draw{\pc@soul@markup}
 - use soul to calculate the dimensions of annotated text
   % \SOUL@{#2}

\pc@soul@markup, def of
 - save start positions of annotation in \pc@Sposx and \pc@Sposy
 - set ending x-postiion, where \ulwidth is the width of annotated text
   % \addtolength{\pc@Eposx}{\ulwidth}
 - set ending y-position to the same as start y-position
 - execute \pc@annot@complete to draw the annotation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants