Is there a way to confine the effect of \XeTeXinterwordspaceshaping=2 to a paragraph within a page

mintedxetex

I am typesetting in Burmese language. Setting \XeTeXinterwordspaceshaping=2 gives me the desired line breaking when paragraphs are fully justified. I've tried several ways, only this option works best for me.

The problem is that I also would like to use minted package for code highlighting and the above option is interfering with minted syntax highlighting. It is mentioned that setting \XeTeXinterwordspaceshaping=2 has side-effects according to this:

https://tug.org/pipermail/xetex/2016-February/026474.html

If that's the case, I was wondering if I can confine the option to only text paragraphs and escape for minted environment. Would that be possible? Here is my MWE(I am sorry that minted requires Python and pygmentize installed):

%!TeX program = XeLaTex
%!TEX encoding = UTF-8 Unicode

\documentclass[10pt]{article}
\XeTeXlinebreaklocale "my_MM"  %Myanmar line and character breaks
\XeTeXinterwordspaceshaping=2 %%% PLEASE EXPERIMENT  BY SETTING WITH DIFFERENT VALUES


\usepackage{fontspec}
\setmainfont{Padauk}

\usepackage{minted}
\begin{document}

သီပေါမင်း (ခရစ်နှစ် ၁၈၇၈-၁၈၈၅) သည် ကုန်းဘောင်မင်းဆက်၏ နောက်ဆုံးမင်းဖြစ်သည်။ သိရီပဝရ ဝိဇယာနန္တ ယသတိလောကာ ဓိပတိ ပဏ္ဍိတ မဟာဓမ္မရာဇာဓိရာဇာ ဖြစ်ပြီး နန်းသက် ခုနစ်နှစ်ဖြစ်သည်။
ကမ္ဘာ့ အကောင်းဆုံး

\XeTeXinterwordspaceshaping=0 %%% PLEASE EXPERIMENT  BY SETTING WITH DIFFERENT VALUES
\begin{minted}{kotlin}
fun main() {
    val name = "stranger1"        // Declare your first variable
    println("Hi, $name!")        // ...and use it!
    print("Current count:")
    for (i in 0..10) {           // Loop over a range from 0 to 10
        print(" $i")
    }
}
\end{minted}
\XeTeXinterwordspaceshaping=2 %%% PLEASE EXPERIMENT  BY SETTING WITH DIFFERENT VALUES
သီပေါမင်း (ခရစ်နှစ် ၁၈၇၈-၁၈၈၅) သည် ကုန်းဘောင်မင်းဆက်၏ နောက်ဆုံးမင်းဖြစ်သည်။ သိရီပဝရ ဝိဇယာနန္တ ယသတိလောကာ ဓိပတိ ပဏ္ဍိတ မဟာဓမ္မရာဇာဓိရာဇာ ဖြစ်ပြီး နန်းသက် ခုနစ်နှစ်ဖြစ်သည်။
ကမ္ဘာ့ အကောင်းဆုံး
\end{document}

Setting 0, I got this output (problematic rendering circled):enter image description here

Setting 2, I got this output (correct rendering in green, unintended side-effect on minted in red)enter image description here

Best Answer

Elsewhere in the email thread that you link to, the behaviour of setting 2 is described as:

These cases are addressed with \XeTeXinterwordspaceshaping=2. With this value, not only are inter-word spaces measured in context, but also each run of text (words and intervening spaces) in a single font will be re-shaped as a unit at \shipout time. This allows full shaping (contextual swashes, ligatures, etc) to take effect across inter-word spaces.

The reference to \shipout means that it acts per-page and can not be changed for individual paragraphs.

But I think this is the output you want (I can't read the script so if this is messed up, leave a comment and I'll try again later)enter image description here

The email you link to gives a clue to use \kern0pt as a workaround to avoid bad effects, so I locally insert a kern at every \special in minted.

\documentclass[10pt]{article}
\XeTeXlinebreaklocale "my_MM"  %Myanmar line and character breaks
\XeTeXinterwordspaceshaping=2 %%% PLEASE EXPERIMENT  BY SETTING WITH DIFFERENT VALUES


\usepackage{fontspec}
\setmainfont{Padauk}
\showoutput
\usepackage{minted}
\begin{document}

သီပေါမင်း (ခရစ်နှစ် ၁၈၇၈-၁၈၈၅) သည် ကုန်းဘောင်မင်းဆက်၏ နောက်ဆုံးမင်းဖြစ်သည်။ သိရီပဝရ ဝိဇယာနန္တ ယသတိလောကာ ဓိပတိ ပဏ္ဍိတ မဟာဓမ္မရာဇာဓိရာဇာ ဖြစ်ပြီး နန်းသက် ခုနစ်နှစ်ဖြစ်သည်။
ကမ္ဘာ့ အကောင်းဆုံး

{
\let\xspecial\special
\def\special{\kern0pt\xspecial}


\begin{minted}{kotlin}
fun main() {
    val name = "stranger1"        // Declare your first variable
    println("Hi, $name!")        // ...and use it!
    print("Current count:")
    for (i in 0..10) {           // Loop over a range from 0 to 10
        print(" $i")
    }
}
\end{minted}

}% end special hack


သီပေါမင်း (ခရစ်နှစ် ၁၈၇၈-၁၈၈၅) သည် ကုန်းဘောင်မင်းဆက်၏ နောက်ဆုံးမင်းဖြစ်သည်။ သိရီပဝရ ဝိဇယာနန္တ ယသတိလောကာ ဓိပတိ ပဏ္ဍိတ မဟာဓမ္မရာဇာဓိရာဇာ ဖြစ်ပြီး နန်းသက် ခုနစ်နှစ်ဖြစ်သည်။
ကမ္ဘာ့ အကောင်းဆုံး
\end{document}
Related Question