mgieseki / dvisvgm

A fast DVI, EPS, and PDF to SVG converter
https://dvisvgm.de
GNU General Public License v3.0
294 stars 28 forks source link

Incorrect position of Chinese with XeLaTeX in TexLive #235

Closed rehnertz closed 1 year ago

rehnertz commented 1 year ago

With the following LaTeX code

\documentclass[preview]{standalone}
\usepackage[UTF8]{ctex}
\begin{document}
    \begin{center}
        Hello 你好 \LaTeX
    \end{center}
\end{document}

The output pdf of xelatex is correct:

pdf-output

However, when transforming xdv to svg, i.e.

xelatex -no-pdf main.tex
dvisvgm -n main.xdv

the Chinese characters are shifted:

svg-output

TexLive2023 on Windodws 11.

I'm using manim to build Maths videos, and I need to write Chinese with inline fomulae. I find out that manim uses dvisvgm -n XXXX.xdv to generate svg's for videos. It does not support outputs without the option -n. Whether or not manim uses other methods to solve this problem, I think this is still a bug in dvisvgm

muzimuzhi commented 1 year ago

Just a workaround, if I feed pdf to dvisvgm then the baseline in svg is fine.

xelatex main.tex
dvisvgm --pdf -n main.pdf

PS: Your main.tex could be simplified to

\documentclass{standalone}
\usepackage{ctex}
\begin{document}
  Hello 你好 \LaTeX
\end{document}
muzimuzhi commented 1 year ago

It seems the mis-alignment only occurs with CJK fonts and the content in CJK fonts is vertically centered on some axis (perhaps the normal baseline), character by character.

% !TeX TS-program = xelatex
\documentclass{standalone}
\usepackage{fontspec}

\font\1="FandolHei"
\font\2="Latin Modern Mono"

\begin{document}
  \parbox{\linewidth}{
    Hello {\fontspec{FandolHei}FandolHei} World {\1FandolHei} \\
    Hello {\fontspec{Latin Modern Mono}LM Mono} World {\2LM Mono}
  }
\end{document}

PDF image SVG (generated from .xdv with -n option) image

mgieseki commented 1 year ago

Thanks for reporting the issue and thanks @muzimuzhi for posting the workaround. I've committed a quick fix that will hopefully work in most cases.