如何在重复执行 \StrSubstitute 时消除空格?

如何在重复执行 \StrSubstitute 时消除空格?

我正在编写一本人造语言的词典,它有多种正字法(相关的正字法是拉丁语和希腊语)。出于各种原因,能够直接从一种语言转换为另一种语言很有帮助。我刚刚用命令完成了从拉丁语到希腊语的转换\greeksubstitution,但当我在其他拉丁语文本中间编译它时,它在希腊语文本之前添加了一个巨大的空格。(作为参考,如果我输入,\textit{Eile areyas llSisera} changes to \greeksubstitution{Eile areyas llSisera}我会得到下面的图片,这很丑陋,根本不是我想要的。)

在此处输入图片描述

我通过使用xstring包(文档这里) 基本上只是对每组相关字符进行查找和替换。编译需要很长时间,但除了下面的巨大空格外,它基本上可以完成工作。我确实注意到,如果我缩短命令以仅替换一个东西(特别是\StrSubstitute{#1}{chu}{ΰ}[\nova]),空格就会消失。有人知道我该如何让它工作吗?谢谢!我将源代码放在下面,并附上一些示例文本,以便在需要时进行摆弄。(仅使用常规英语是行不通的,因为该命令没有 j、k、q、w、x 或 z 的替换,因此所有这些都必须被过滤掉,我认为这会更容易。)

谢谢!!

\documentclass{article}
\usepackage{xstring}
\usepackage{fontspec}
\usepackage{lipsum}
\setmainfont{CMU Serif}
\newcommand{\greeksubstitution}[1]{
\StrSubstitute{#1}{chy}{ΐ}[\nova]
\StrSubstitute{\nova}{CHY}{ΧΪ}[\nova]
\StrSubstitute{\nova}{cHY}{ΧΪ}[\nova]
\StrSubstitute{\nova}{ChY}{ΧΪ}[\nova]
\StrSubstitute{\nova}{CHy}{ΧΪ}[\nova]
\StrSubstitute{\nova}{Chy}{ΧΪ}[\nova]
\StrSubstitute{\nova}{cHy}{ΧΪ}[\nova]
\StrSubstitute{\nova}{chY}{ΧΪ}[\nova]%CHU
\StrSubstitute{\nova}{chu}{ΰ}[\nova]
\StrSubstitute{\nova}{CHU}{ΧΫ}[\nova]
\StrSubstitute{\nova}{cHU}{ΧΫ}[\nova]
\StrSubstitute{\nova}{ChU}{ΧΫ}[\nova]
\StrSubstitute{\nova}{CHu}{ΧΫ}[\nova]
\StrSubstitute{\nova}{Chu}{ΧΫ}[\nova]
\StrSubstitute{\nova}{cHu}{ΧΫ}[\nova]
\StrSubstitute{\nova}{chU}{ΧΫ}[\nova]%Others
\StrSubstitute{\nova}{si}{ςι}[\nova]\StrSubstitute{\nova}{SI}{ΣΙ}[\nova]\StrSubstitute{\nova}{sI}{ςΙ}[\nova]\StrSubstitute{\nova}{Si}{Σι}[\nova]
\StrSubstitute{\nova}{bh}{ϋ}[\nova]\StrSubstitute{\nova}{BH}{Ϋ}[\nova]\StrSubstitute{\nova}{bH}{Ϋ}[\nova]\StrSubstitute{\nova}{Bh}{Ϋ}[\nova]
\StrSubstitute{\nova}{nh}{ψ}[\nova]\StrSubstitute{\nova}{NH}{Ψ}[\nova]\StrSubstitute{\nova}{nH}{Ψ}[\nova]\StrSubstitute{\nova}{Nh}{Ψ}[\nova]
\StrSubstitute{\nova}{ll}{θ}[\nova]\StrSubstitute{\nova}{LL}{Θ}[\nova]\StrSubstitute{\nova}{Ll}{Θ}[\nova]\StrSubstitute{\nova}{Ll}{Θ}[\nova]
\StrSubstitute{\nova}{ng}{ξ}[\nova]\StrSubstitute{\nova}{NG}{Ξ}[\nova]\StrSubstitute{\nova}{nG}{Ξ}[\nova]\StrSubstitute{\nova}{Ng}{Ξ}[\nova]
\StrSubstitute{\nova}{ch}{χ}[\nova]\StrSubstitute{\nova}{CH}{Χ}[\nova]\StrSubstitute{\nova}{cH}{Χ}[\nova]\StrSubstitute{\nova}{Ch}{Χ}[\nova]
\StrSubstitute{\nova}{lh}{ω}[\nova]\StrSubstitute{\nova}{LH}{Ω}[\nova]\StrSubstitute{\nova}{lH}{Ω}[\nova]\StrSubstitute{\nova}{Lh}{Ω}[\nova]
\StrSubstitute{\nova}{ha}{ά}[\nova]\StrSubstitute{\nova}{HA}{Ά}[\nova]\StrSubstitute{\nova}{hA}{Ά}[\nova]\StrSubstitute{\nova}{Ha}{Ά}[\nova]
\StrSubstitute{\nova}{he}{έ}[\nova]\StrSubstitute{\nova}{HE}{Έ}[\nova]\StrSubstitute{\nova}{hE}{Έ}[\nova]\StrSubstitute{\nova}{He}{Έ}[\nova]
\StrSubstitute{\nova}{hi}{ί}[\nova]\StrSubstitute{\nova}{HI}{Ί}[\nova]\StrSubstitute{\nova}{hI}{Ί}[\nova]\StrSubstitute{\nova}{Hi}{Ί}[\nova]
\StrSubstitute{\nova}{ho}{ό}[\nova]\StrSubstitute{\nova}{HO}{Ό}[\nova]\StrSubstitute{\nova}{hO}{Ό}[\nova]\StrSubstitute{\nova}{Ho}{Ό}[\nova]
\StrSubstitute{\nova}{a}{α}[\nova]\StrSubstitute{\nova}{A}{Α}[\nova]
\StrSubstitute{\nova}{e}{ε}[\nova]\StrSubstitute{\nova}{E}{Ε}[\nova]
\StrSubstitute{\nova}{i}{ι}[\nova]\StrSubstitute{\nova}{I}{Ι}[\nova]
\StrSubstitute{\nova}{o}{ο}[\nova]\StrSubstitute{\nova}{O}{Ο}[\nova]
\StrSubstitute{\nova}{p}{π}[\nova]\StrSubstitute{\nova}{P}{Π}[\nova]
\StrSubstitute{\nova}{b}{β}[\nova]\StrSubstitute{\nova}{B}{Β}[\nova]
\StrSubstitute{\nova}{m}{μ}[\nova]\StrSubstitute{\nova}{M}{Μ}[\nova]
\StrSubstitute{\nova}{f}{φ}[\nova]\StrSubstitute{\nova}{F}{Φ}[\nova]
\StrSubstitute{\nova}{v}{υ}[\nova]\StrSubstitute{\nova}{V}{Υ}[\nova]
\StrSubstitute{\nova}{t}{τ}[\nova]\StrSubstitute{\nova}{T}{Τ}[\nova]
\StrSubstitute{\nova}{d}{δ}[\nova]\StrSubstitute{\nova}{D}{Δ}[\nova]
\StrSubstitute{\nova}{n}{ν}[\nova]\StrSubstitute{\nova}{N}{Ν}[\nova]
\StrSubstitute{\nova}{s}{σ}[\nova]\StrSubstitute{\nova}{S}{Σ}[\nova]
\StrSubstitute{\nova}{r}{ρ}[\nova]\StrSubstitute{\nova}{R}{Ρ}[\nova]
\StrSubstitute{\nova}{l}{λ}[\nova]\StrSubstitute{\nova}{L}{Λ}[\nova]
\StrSubstitute{\nova}{c}{κ}[\nova]\StrSubstitute{\nova}{C}{Κ}[\nova]
\StrSubstitute{\nova}{g}{γ}[\nova]\StrSubstitute{\nova}{G}{Γ}[\nova]
\StrSubstitute{\nova}{y}{ϊ}[\nova]\StrSubstitute{\nova}{Y}{Ϊ}[\nova]
\StrSubstitute{\nova}{u}{ϋ}[\nova]\StrSubstitute{\nova}{U}{Ϋ}[\nova]
\nova
}
\begin{document}
\begin{raggedright}
Eile areyas llSisera changes to \greeksubstitution{Eile areyas llSisera}
\end{raggedright}
\end{document}

答案1

您需要使用(注释)字符\greeksubstitution来终止宏内的每一行%,否则(不可见的)行尾字符会被 TeX 转换为空格。

例如,改变

\newcommand{\greeksubstitution}[1]{
\StrSubstitute{#1}{chy}{ΐ}[\nova]
\StrSubstitute{\nova}{CHY}{ΧΪ}[\nova]
\StrSubstitute{\nova}{cHY}{ΧΪ}[\nova]
\StrSubstitute{\nova}{ChY}{ΧΪ}[\nova]

\newcommand{\greeksubstitution}[1]{%
\StrSubstitute{#1}{chy}{ΐ}[\nova]%
\StrSubstitute{\nova}{CHY}{ΧΪ}[\nova]%
\StrSubstitute{\nova}{cHY}{ΧΪ}[\nova]%
\StrSubstitute{\nova}{ChY}{ΧΪ}[\nova]%

等等。

在此处输入图片描述

\documentclass{article}
\usepackage{xstring}
\usepackage{fontspec}
\setmainfont{CMU Serif}

\newcommand{\greeksubstitution}[1]{%
\StrSubstitute{#1}{chy}{ΐ}[\nova]%
\StrSubstitute{\nova}{CHY}{ΧΪ}[\nova]%
\StrSubstitute{\nova}{cHY}{ΧΪ}[\nova]%
\StrSubstitute{\nova}{ChY}{ΧΪ}[\nova]%
\StrSubstitute{\nova}{CHy}{ΧΪ}[\nova]%
\StrSubstitute{\nova}{Chy}{ΧΪ}[\nova]%
\StrSubstitute{\nova}{cHy}{ΧΪ}[\nova]%
\StrSubstitute{\nova}{chY}{ΧΪ}[\nova]%CHU
\StrSubstitute{\nova}{chu}{ΰ}[\nova]%
\StrSubstitute{\nova}{CHU}{ΧΫ}[\nova]%
\StrSubstitute{\nova}{cHU}{ΧΫ}[\nova]%
\StrSubstitute{\nova}{ChU}{ΧΫ}[\nova]%
\StrSubstitute{\nova}{CHu}{ΧΫ}[\nova]%
\StrSubstitute{\nova}{Chu}{ΧΫ}[\nova]%
\StrSubstitute{\nova}{cHu}{ΧΫ}[\nova]%
\StrSubstitute{\nova}{chU}{ΧΫ}[\nova]%Others
\StrSubstitute{\nova}{si}{ςι}[\nova]\StrSubstitute{\nova}{SI}{ΣΙ}[\nova]\StrSubstitute{\nova}{sI}{ςΙ}[\nova]\StrSubstitute{\nova}{Si}{Σι}[\nova]%
\StrSubstitute{\nova}{bh}{ϋ}[\nova]\StrSubstitute{\nova}{BH}{Ϋ}[\nova]\StrSubstitute{\nova}{bH}{Ϋ}[\nova]\StrSubstitute{\nova}{Bh}{Ϋ}[\nova]%
\StrSubstitute{\nova}{nh}{ψ}[\nova]\StrSubstitute{\nova}{NH}{Ψ}[\nova]\StrSubstitute{\nova}{nH}{Ψ}[\nova]\StrSubstitute{\nova}{Nh}{Ψ}[\nova]%
\StrSubstitute{\nova}{ll}{θ}[\nova]\StrSubstitute{\nova}{LL}{Θ}[\nova]\StrSubstitute{\nova}{Ll}{Θ}[\nova]\StrSubstitute{\nova}{Ll}{Θ}[\nova]%
\StrSubstitute{\nova}{ng}{ξ}[\nova]\StrSubstitute{\nova}{NG}{Ξ}[\nova]\StrSubstitute{\nova}{nG}{Ξ}[\nova]\StrSubstitute{\nova}{Ng}{Ξ}[\nova]%
\StrSubstitute{\nova}{ch}{χ}[\nova]\StrSubstitute{\nova}{CH}{Χ}[\nova]\StrSubstitute{\nova}{cH}{Χ}[\nova]\StrSubstitute{\nova}{Ch}{Χ}[\nova]%
\StrSubstitute{\nova}{lh}{ω}[\nova]\StrSubstitute{\nova}{LH}{Ω}[\nova]\StrSubstitute{\nova}{lH}{Ω}[\nova]\StrSubstitute{\nova}{Lh}{Ω}[\nova]%
\StrSubstitute{\nova}{ha}{ά}[\nova]\StrSubstitute{\nova}{HA}{Ά}[\nova]\StrSubstitute{\nova}{hA}{Ά}[\nova]\StrSubstitute{\nova}{Ha}{Ά}[\nova]%
\StrSubstitute{\nova}{he}{έ}[\nova]\StrSubstitute{\nova}{HE}{Έ}[\nova]\StrSubstitute{\nova}{hE}{Έ}[\nova]\StrSubstitute{\nova}{He}{Έ}[\nova]%
\StrSubstitute{\nova}{hi}{ί}[\nova]\StrSubstitute{\nova}{HI}{Ί}[\nova]\StrSubstitute{\nova}{hI}{Ί}[\nova]\StrSubstitute{\nova}{Hi}{Ί}[\nova]%
\StrSubstitute{\nova}{ho}{ό}[\nova]\StrSubstitute{\nova}{HO}{Ό}[\nova]\StrSubstitute{\nova}{hO}{Ό}[\nova]\StrSubstitute{\nova}{Ho}{Ό}[\nova]%
\StrSubstitute{\nova}{a}{α}[\nova]\StrSubstitute{\nova}{A}{Α}[\nova]%
\StrSubstitute{\nova}{e}{ε}[\nova]\StrSubstitute{\nova}{E}{Ε}[\nova]%
\StrSubstitute{\nova}{i}{ι}[\nova]\StrSubstitute{\nova}{I}{Ι}[\nova]%
\StrSubstitute{\nova}{o}{ο}[\nova]\StrSubstitute{\nova}{O}{Ο}[\nova]%
\StrSubstitute{\nova}{p}{π}[\nova]\StrSubstitute{\nova}{P}{Π}[\nova]%
\StrSubstitute{\nova}{b}{β}[\nova]\StrSubstitute{\nova}{B}{Β}[\nova]%
\StrSubstitute{\nova}{m}{μ}[\nova]\StrSubstitute{\nova}{M}{Μ}[\nova]%
\StrSubstitute{\nova}{f}{φ}[\nova]\StrSubstitute{\nova}{F}{Φ}[\nova]%
\StrSubstitute{\nova}{v}{υ}[\nova]\StrSubstitute{\nova}{V}{Υ}[\nova]%
\StrSubstitute{\nova}{t}{τ}[\nova]\StrSubstitute{\nova}{T}{Τ}[\nova]%
\StrSubstitute{\nova}{d}{δ}[\nova]\StrSubstitute{\nova}{D}{Δ}[\nova]%
\StrSubstitute{\nova}{n}{ν}[\nova]\StrSubstitute{\nova}{N}{Ν}[\nova]%
\StrSubstitute{\nova}{s}{σ}[\nova]\StrSubstitute{\nova}{S}{Σ}[\nova]%
\StrSubstitute{\nova}{r}{ρ}[\nova]\StrSubstitute{\nova}{R}{Ρ}[\nova]%
\StrSubstitute{\nova}{l}{λ}[\nova]\StrSubstitute{\nova}{L}{Λ}[\nova]%
\StrSubstitute{\nova}{c}{κ}[\nova]\StrSubstitute{\nova}{C}{Κ}[\nova]%
\StrSubstitute{\nova}{g}{γ}[\nova]\StrSubstitute{\nova}{G}{Γ}[\nova]%
\StrSubstitute{\nova}{y}{ϊ}[\nova]\StrSubstitute{\nova}{Y}{Ϊ}[\nova]%
\StrSubstitute{\nova}{u}{ϋ}[\nova]\StrSubstitute{\nova}{U}{Ϋ}[\nova]%
\nova%
}

\begin{document}
\raggedright
Eile areyas llSisera changes to \greeksubstitution{Eile areyas llSisera}.
\end{document}

答案2

问题在于行尾被转换为空格。这里有一个不同的实现,expl3它以某种方式减少了检查次数。

\documentclass{article}
\usepackage{fontspec}

\setmainfont{CMU Serif}

\ExplSyntaxOn

\NewDocumentCommand{\greeksubstitution}{m}
 {
  \tl_set:Nn \l_tmpa_tl { #1 }
  % chy
  \regex_replace_all:nnN {chy} {ΐ} \l_tmpa_tl
  \regex_replace_all:nnN {[Cc][Hh][Yy]}{ΧΪ} \l_tmpa_tl
  % chu
  \regex_replace_all:nnN {chu} {ΰ} \l_tmpa_tl
  \regex_replace_all:nnN {[Cc][Hh][Uu]}{ΧΫ} \l_tmpa_tl
  % si
  \regex_replace_all:nnN {si} {ςι} \l_tmpa_tl
  \regex_replace_all:nnN {sI} {ςΙ} \l_tmpa_tl
  % bh
  \regex_replace_all:nnN {bh}{ϋ} \l_tmpa_tl
  \regex_replace_all:nnN {[Bb][Hh]}{Ϋ} \l_tmpa_tl
  % nh
  \regex_replace_all:nnN {nh}{ψ} \l_tmpa_tl
  \regex_replace_all:nnN {[Nn][Hh]}{Ψ} \l_tmpa_tl
  % ll
  \regex_replace_all:nnN {ll}{θ} \l_tmpa_tl
  \regex_replace_all:nnN {[Ll][Ll]}{Θ} \l_tmpa_tl
  % ng
  \regex_replace_all:nnN {ng}{ξ} \l_tmpa_tl
  \regex_replace_all:nnN {[Nn][Gg]}{Ξ} \l_tmpa_tl
  % ch
  \regex_replace_all:nnN {ch}{χ} \l_tmpa_tl
  \regex_replace_all:nnN {[Cc][Hh]}{Χ} \l_tmpa_tl
  % lh
  \regex_replace_all:nnN {lh}{ω} \l_tmpa_tl
  \regex_replace_all:nnN {[Ll][Hh]}{Ω} \l_tmpa_tl
  % ha
  \regex_replace_all:nnN {ha}{ά} \l_tmpa_tl
  \regex_replace_all:nnN {[Hh][Aa]}{Ά} \l_tmpa_tl
  % he
  \regex_replace_all:nnN {he}{έ} \l_tmpa_tl
  \regex_replace_all:nnN {[Hh][Ee]}{Έ} \l_tmpa_tl
  % hi
  \regex_replace_all:nnN {hi}{ί} \l_tmpa_tl
  \regex_replace_all:nnN {[Hh][Ii]}{Ί} \l_tmpa_tl
  % ho
  \regex_replace_all:nnN {ho}{ό} \l_tmpa_tl
  \regex_replace_all:nnN {[Hh][Oo]}{Ό} \l_tmpa_tl
  % simple letters
  \regex_replace_all:nnN {a}{α} \l_tmpa_tl
  \regex_replace_all:nnN {A}{Α} \l_tmpa_tl
  \regex_replace_all:nnN {e}{ε} \l_tmpa_tl
  \regex_replace_all:nnN {E}{Ε} \l_tmpa_tl
  \regex_replace_all:nnN {i}{ι} \l_tmpa_tl
  \regex_replace_all:nnN {I}{Ι} \l_tmpa_tl
  \regex_replace_all:nnN {o}{ο} \l_tmpa_tl
  \regex_replace_all:nnN {O}{Ο} \l_tmpa_tl
  \regex_replace_all:nnN {p}{π} \l_tmpa_tl
  \regex_replace_all:nnN {P}{Π} \l_tmpa_tl
  \regex_replace_all:nnN {b}{β} \l_tmpa_tl
  \regex_replace_all:nnN {B}{Β} \l_tmpa_tl
  \regex_replace_all:nnN {m}{μ} \l_tmpa_tl
  \regex_replace_all:nnN {M}{Μ} \l_tmpa_tl
  \regex_replace_all:nnN {f}{φ} \l_tmpa_tl
  \regex_replace_all:nnN {F}{Φ} \l_tmpa_tl
  \regex_replace_all:nnN {v}{υ} \l_tmpa_tl
  \regex_replace_all:nnN {V}{Υ} \l_tmpa_tl
  \regex_replace_all:nnN {t}{τ} \l_tmpa_tl
  \regex_replace_all:nnN {T}{Τ} \l_tmpa_tl
  \regex_replace_all:nnN {d}{δ} \l_tmpa_tl
  \regex_replace_all:nnN {D}{Δ} \l_tmpa_tl
  \regex_replace_all:nnN {n}{ν} \l_tmpa_tl
  \regex_replace_all:nnN {N}{Ν} \l_tmpa_tl
  \regex_replace_all:nnN {s}{σ} \l_tmpa_tl
  \regex_replace_all:nnN {S}{Σ} \l_tmpa_tl
  \regex_replace_all:nnN {r}{ρ} \l_tmpa_tl
  \regex_replace_all:nnN {R}{Ρ} \l_tmpa_tl
  \regex_replace_all:nnN {l}{λ} \l_tmpa_tl
  \regex_replace_all:nnN {L}{Λ} \l_tmpa_tl
  \regex_replace_all:nnN {c}{κ} \l_tmpa_tl
  \regex_replace_all:nnN {C}{Κ} \l_tmpa_tl
  \regex_replace_all:nnN {g}{γ} \l_tmpa_tl
  \regex_replace_all:nnN {G}{Γ} \l_tmpa_tl
  \regex_replace_all:nnN {y}{ϊ} \l_tmpa_tl
  \regex_replace_all:nnN {Y}{Ϊ} \l_tmpa_tl
  \regex_replace_all:nnN {u}{ϋ} \l_tmpa_tl
  \regex_replace_all:nnN {U}{Ϋ} \l_tmpa_tl
  \tl_use:N \l_tmpa_tl
}

\ExplSyntaxOff

\begin{document}

\begin{flushleft}
Eile areyas llSisera changes to \greeksubstitution{Eile areyas llSisera}

\greeksubstitution{chy cHy Chu chu sI st}
\end{flushleft}

\end{document}

在此处输入图片描述

相关内容