1

(Note: This is a follow-up to this question)

I am using cgloss4e to write down some language examples. Some of these examples do not fit within a single line, therefore I am trying to distribute their content on multiple lines which, however, belong to the same numbered example.

Problem. When i distribute the content onto multiple lines, the spacing among the paragraphs seems to change in an unexpected way.

Example.

\documentclass{report}

\usepackage{xcolor}
\usepackage{polyglossia}
\usepackage[AutoFallBack=true]{xeCJK}[2018/02/27]
\setCJKmainfont[FallBack=NotoSansCJKtc-Regular]{NotoSerifCJKtc-Regular}
    % NOTE: download NotoSerifCJKtc-Regular font from here
    %       https://www.google.com/get/noto/help/cjk/
\usepackage{xpinyin}
\usepackage{gb4e}

\let\eachwordtwo=\pinyin
\newcommand{\glit}[1]{\nobreak\glt {\it ``{#1}''}}

\makeatletter
\newcommand*\glosscolor[1]{\gdef\@glcolor{#1}}
\glosscolor{red}
\AtBeginDocument{\colorlet{savedcol}{.}}
\def\@glosscolor{savedcol}

\newcommand{\clt}{\gdef\@glosscolor{\@glcolor}\color{\@glosscolor}}
\newcommand{\clf}{\gdef\@glosscolor{savedcol}\color{\@glosscolor}}

\gdef\getwords(#1,#2)#3 #4\\% #1=linebox, #2=\each, #3=1st word, #4=remainder
   {\setbox#1=\vbox{\hbox{#2{\strut\color{\@glosscolor}#3} }% adds space
                    \unvbox#1%
                   }%
    \def\more{#4}%
    \ifx\more\empty\let\more=\donewords
    \else\let\more=\getwords
    \fi
    \more(#1,#2)#4\\%
   }
\gdef\donewords(#1,#2)\\{\gdef\@glosscolor{savedcol}}   
\makeatother


\begin{document}

Normal spacing:

\begin{exe}
\ex
  \glll
    你  \clt 為甚麼      \clf 學    中文       ? \\ 
    ni3 \clt wei4shen2me \clf xue2  Zhong1wen2 ?  \\ 
    You \clt why \clf study Chinese ?  \\
  \glit
    {Why do you study Chinese?}
\ex
  \glll
    \clt 因為     \clf 我  在   中國       工作 \\
    \clt Yin1wei4 \clf wo3 zai4 Zhong1guo2 gong1zuo4 \\
    \clt Because  \clf I   in   China      working \\
\glit
    {Because I am working in China}
\end{exe}

Incorrect spacing:

\begin{exe}
\ex
  \glll
    你  \clt 為甚麼      \clf 學    中文       ? \\ 
    ni3 \clt wei4shen2me \clf xue2  Zhong1wen2 ?  \\ 
    You \clt why         \clf study Chinese ?  \\
  \glit
    {Why do you study Chinese?}
  \glll
    \clt 因為     \clf 我  在   中國       工作 \\
    \clt Yin1wei4 \clf wo3 zai4 Zhong1guo2 gong1zuo4 \\
    \clt Because  \clf I   in   China      working \\
\glit
    {Because I am working in China}
\end{exe}

\end{document}

Output.

enter image description here

Normally, the line enclosed by the glit command is placed immediately below the three-lines example.

However, when I remove ex because I want multiple three-lines with the same number, the spacing changes. In the picture above, it can be clearly seen that the sentence ''Because I am working in China'' is placed further away from its corresponding text.

Ideally, I would like to be able to make multiple glll groups belong to the same ex numbered example without these spacing issues.

Question. Why is this happening, and how can I fix this?

  • I’ve written an answer below. Note: SimSun is a trademark of ZHONGYI Electronic Co., Beijing, which is licensed to Microsoft exclusively. Please remove the download website information in your MWE. – Ruixi Zhang Jul 12 '18 at 5:38
  • @RuixiZhang thank you very much for letting me know. I'll fix the code ASAP. – Patrick Trentin Jul 12 '18 at 6:54
  • I added a cleaner solution. Also, I provided additional information on Open Source Fonts Collection for Chinese at the end. :) – Ruixi Zhang Jul 12 '18 at 16:40
  • @RuixiZhang can't thank you enough for warning me about fonts usage, and also for the nice answer. I fixed the code/example in this question and I will do the same for previous questions of mine shortly after. – Patrick Trentin Jul 12 '18 at 16:53
1

The unwanted vertical spacing is caused by the flushleft environment. See this answer for a description.

New answer

Since we are patching a lot of commands, I’d recommend using the etoolbox package. This approach makes your preamble code cleaner and easier to follow. And, you don’t need to copy the full original definition from cgloss4e.sty to make your adjustments.

\documentclass{report}

\usepackage{xcolor}
\usepackage[english]{babel}
% Not needed for this problem
%\usepackage{polyglossia}
%\usepackage[AutoFallBack=true]{xeCJK}[2018/02/27]
%\setCJKmainfont[FallBack=SimSun-ExtB]{SimSun}
    % Removed!
    % Removed!
    % Removed!
\usepackage{xpinyin}
\usepackage{gb4e}

\let\eachwordtwo=\pinyin
%\newcommand{\glit}[1]{\nobreak\glt {\it ``{#1}''}}
% I changed your original definition of \glit:
\newcommand{\glit}[1]{\glt ``\emph{#1}''\par}

\usepackage{etoolbox}% https://ctan.org/pkg/etoolbox
\makeatletter% Group everything to save the trouble
% My patches:
\patchcmd{\gll}% The command to be patched
  {\begin{flushleft}}% Code to be replaced
  {\begingroup\raggedright}% New code
  {}{}
\patchcmd{\glll}% The command to be patched
  {\begin{flushleft}}% Code to be replaced
  {\begingroup\raggedright}% New code
  {}{}
\patchcmd{\gl@stop}% The command to be patched
  {\end{flushleft}}% Code to be replaced
  {\endgroup}% New code
  {}{}
% Your color customization:
\newcommand*\glosscolor[1]{\gdef\@glcolor{#1}}
\glosscolor{red}
\AtBeginDocument{\colorlet{savedcol}{.}}
\def\@glosscolor{savedcol}
\newcommand{\clt}{\gdef\@glosscolor{\@glcolor}\color{\@glosscolor}}
\newcommand{\clf}{\gdef\@glosscolor{savedcol}\color{\@glosscolor}}
% Your patch written in the style of \patchcmd:
\patchcmd{\getwords}% The command to be patched
  {\strut#3}% Code to be replaced
  {\strut\color{\@glosscolor}#3}% New code
  {}{}
\gdef\donewords(#1,#2)\\{\gdef\@glosscolor{savedcol}}
\makeatother


\begin{document}

Normal spacing:

\begin{exe}
\ex
  \glll
    a   \clt aaa         \clf a     aa         ? \\
    ni3 \clt wei4shen2me \clf xue2  Zhong1wen2 ? \\
    You \clt why         \clf study Chinese    ? \\
  \glit
    {Why do you study Chinese?}
\ex
  \glll
    \clt aa       \clf a   a    aa         aa        \\
    \clt Yin1wei4 \clf wo3 zai4 Zhong1guo2 gong1zuo4 \\
    \clt Because  \clf I   in   China      working   \\
  \glit
    {Because I am working in China}
\end{exe}

Incorrect spacing (now corrected):

\begin{exe}
\ex
  \glll
    a   \clt aaa         \clf a     aa         ? \\
    ni3 \clt wei4shen2me \clf xue2  Zhong1wen2 ? \\
    You \clt why         \clf study Chinese    ? \\
  \glit
    {Why do you study Chinese?}
  \glll
    \clt aa       \clf a   a    aa         aa        \\
    \clt Yin1wei4 \clf wo3 zai4 Zhong1guo2 gong1zuo4 \\
    \clt Because  \clf I   in   China      working   \\
  \glit
    {Because I am working in China}
\end{exe}

\end{document}

Original answer

To get around this issue, I used a patch:

\makeatletter
\def\gll%                 % Introduces 2-line text-and-gloss.
   {\begingroup\raggedright
     \ifx\@gsingle1%           conditionally force single spacing (hpk/MC)
        \vskip\baselineskip\def\baselinestretch{1}%
        \@selfnt\vskip-\baselineskip\fi%
    \bgroup
    \twosent
   }
\def\glll%                 % Introduces 3-line text-and-gloss.
   {\begingroup\raggedright
     \ifx\@gsingle1%        conditionally force single spacing (hpk/MC)
        \vskip\baselineskip\def\baselinestretch{1}%
        \@selfnt\vskip-\baselineskip\fi%
    \bgroup
    \threesent
   }
\def\gl@stop{{\hskip -\glossglue}\unhbox\gline\endgroup}
\makeatother

To get proper paragraph break, I also changed your definition of \glit:

\newcommand{\glit}[1]{\nobreak\glt {\it ``{#1}''}\par}

A full MWE:

\documentclass{report}

\usepackage{xcolor}
\usepackage[english]{babel}
% Not needed for your problem
%\usepackage{polyglossia}
%\usepackage[AutoFallBack=true]{xeCJK}[2018/02/27]
%\setCJKmainfont[FallBack=SimSun-ExtB]{SimSun}
    % Removed!
    % Removed!
    % Removed!
\usepackage{xpinyin}
\usepackage{gb4e}

\let\eachwordtwo=\pinyin
%\newcommand{\glit}[1]{\nobreak\glt {\it ``{#1}''}}
% I changed your original definition
\newcommand{\glit}[1]{\nobreak\glt {\it ``{#1}''}\par}

\makeatletter
% My patches:
\def\gll%                 % Introduces 2-line text-and-gloss.
   {\begingroup\raggedright
     \ifx\@gsingle1%           conditionally force single spacing (hpk/MC)
        \vskip\baselineskip\def\baselinestretch{1}%
        \@selfnt\vskip-\baselineskip\fi%
    \bgroup
    \twosent
   }
\def\glll%                 % Introduces 3-line text-and-gloss.
   {\begingroup\raggedright
     \ifx\@gsingle1%        conditionally force single spacing (hpk/MC)
        \vskip\baselineskip\def\baselinestretch{1}%
        \@selfnt\vskip-\baselineskip\fi%
    \bgroup
    \threesent
   }
\def\gl@stop{{\hskip -\glossglue}\unhbox\gline\endgroup}
% Your patches:
\newcommand*\glosscolor[1]{\gdef\@glcolor{#1}}
\glosscolor{red}
\AtBeginDocument{\colorlet{savedcol}{.}}
\def\@glosscolor{savedcol}
\newcommand{\clt}{\gdef\@glosscolor{\@glcolor}\color{\@glosscolor}}
\newcommand{\clf}{\gdef\@glosscolor{savedcol}\color{\@glosscolor}}
\gdef\getwords(#1,#2)#3 #4\\% #1=linebox, #2=\each, #3=1st word, #4=remainder
   {\setbox#1=\vbox{\hbox{#2{\strut\color{\@glosscolor}#3} }% adds space
                    \unvbox#1%
                   }%
    \def\more{#4}%
    \ifx\more\empty\let\more=\donewords
    \else\let\more=\getwords
    \fi
    \more(#1,#2)#4\\%
   }
\gdef\donewords(#1,#2)\\{\gdef\@glosscolor{savedcol}}   
\makeatother


\begin{document}

Normal spacing:

\begin{exe}
\ex
  \glll
    a  \clt aaa      \clf a    aa       ? \\ 
    ni3 \clt wei4shen2me \clf xue2  Zhong1wen2 ?  \\ 
    You \clt why \clf study Chinese ?  \\
  \glit
    {Why do you study Chinese?}
\ex
  \glll
    \clt aa     \clf a  a   aa       aa \\
    \clt Yin1wei4 \clf wo3 zai4 Zhong1guo2 gong1zuo4 \\
    \clt Because  \clf I   in   China      working \\
\glit
    {Because I am working in China}
\end{exe}

Incorrect spacing:

\begin{exe}
\ex
  \glll
    a  \clt aaa      \clf a    aa       ? \\ 
    ni3 \clt wei4shen2me \clf xue2  Zhong1wen2 ?  \\ 
    You \clt why         \clf study Chinese ?  \\
  \glit
    {Why do you study Chinese?}
  \glll
    \clt aa     \clf a  a   aa       aa \\
    \clt Yin1wei4 \clf wo3 zai4 Zhong1guo2 gong1zuo4 \\
    \clt Because  \clf I   in   China      working \\
\glit
    {Because I am working in China}
\end{exe}

\end{document}

Spacing correct

Side note

SimSun is a trademark of ZHONGYI Electronic Co., Beijing, which is licensed to Microsoft exclusively. Any use of SimSun outside the Windows operating system may be considered as trademark infringement. If you want a “serif” looking CJK font, please consider using the Source Han Serif font released in 2017, which is free and open sourced. You can also download Source Han Serif from the Google Noto Fonts website.

If you want to use other open sourced CJK fonts, take a look at Open Source Fonts Collection for Chinese (website in HK Traditional Chinese, judging from the stroke of your character).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.