新的命令定义带来额外的空间

新的命令定义带来额外的空间

抱歉,我原来的示例代码不能准确反映我的意思。我已经做了一个新的。

我正在尝试定义一个新命令,但似乎生成了一些不需要的额外空格。你能告诉我如何删除它们吗?

测试代码:

\documentclass{article}

\usepackage{mulan}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\entrue 
\jptrue 
\cnfalse

\begin{document}

\title{\mulanp{Multilingual Document in English }{Multilingual Document in Japanese}{Multilingual Document in Chinese}}
\author{\mulanr{Name in English}{Name in Japanese}{Name in Chinese}}

\maketitle 

\section{Title in English/Title in Japanese/Title in Chinese}
\section{\mulanr{Title in English}{Title in Japanese}{Title in Chinese}}

Example

\end{document}

我这里使用的包如下。

% Multi-language support package<mulan.sty>
% 
% usage: 
% \mulan[en,jp,cn,...]{seperator}{before}{after}{en-sentence}{jp-sentence}{cn-sentence}
%
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mulan}[2015/02/23 Multi-language support package version 0.0.1] 
% if the language sequence is not given, 
% given global language sequence \mulanseq will be used  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\Chinese{}  %used to specify Chinese font
\newcommand\Japanese{} %used to specify Japanese font
\newcommand\Korean{}   %used to specify Korean font
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifen \newif\iffr \newif\ifru \newif\ifjp \newif\ifcn \newif\ifkr
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\en#1{\ifen\callsep #1\fi\langcmd} %
\def\fr#1{\iffr\callsep #1\fi\langcmd} %
\def\ru#1{\ifru\callsep #1\fi\langcmd} %
\def\jp#1{\ifjp\callsep {\Japanese #1}\fi\langcmd} %
\def\cn#1{\ifcn\callsep {\Chinese #1}\fi\langcmd} %
\def\kr#1{\ifkr\callsep {\Korean #1}\fi\langcmd} %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{mulan@args}
\newcounter{mulan@argsmax}

\newcommand\mulan[4][en,jp,cn]{%  
  \gdef\mulan@thesep{#2}%
    \gdef\mulan@thebefore{#3} %     
    \gdef\mulan@theafter{#4} %  
  \setcounter{mulan@args}{0}%
  \setcounter{mulan@argsmax}{0}%        
  \commaparse#1,\relax%
  \stepcounter{mulan@args}%
  \expandafter\def\csname arg\romannumeral\value{mulan@args}\endcsname{relax}%
  \setcounter{mulan@args}{0}%       
    \mulan@thebefore
    \langcmd%   
}

\def\commaparse#1,#2\relax{%
  \stepcounter{mulan@args}%
    \ifnum\value{mulan@args}>\value{mulan@argsmax} \setcounter{mulan@argsmax}{\value{mulan@args}} \fi  %get the max number of arguments is 
  \expandafter\def\csname arg\romannumeral\value{mulan@args}\endcsname{#1}%
  \if\relax#2\else\commaparse#2\relax\fi%
}

\newcommand\langcmd{%
  \stepcounter{mulan@args}%
    \ifnum\value{mulan@argsmax}<\value{mulan@args} \mulan@theafter \fi %
  \csname\csname arg\romannumeral\value{mulan@args}\endcsname\endcsname%
}

\newcommand\callsep{\ifnum\value{mulan@args}>1\mulan@thesep\fi} %

%%%%=======================================================
\def\mulanp{\expandafter\mulan{\par}{}{}}
\def\mulanr{\expandafter\mulan{ \space {\textbf{/}}\space}{}{}}
\def\mulanTL{ \mulan{\\}{\begin{tabular}{@{}l@{}}}{\end{tabular}} }
\def\mulanTC{ \mulan{\\}{\begin{tabular}{@{}c@{}}}{\end{tabular}} }
\def\mulanTR{ \mulan{\\}{\begin{tabular}{@{}r@{}}}{\end{tabular}} }

有点长,难以理解。请参考帖子以了解<解析以逗号分隔的命令参数>

答案1

我不知道你为什么要这个,但是......

\documentclass{article}

\newcommand\mulan[5]{%
    #1#4#2#5#3%
}%

\newcommand\mulanr[2]{%
\mulan{\ignorespaces}{/}{\ignorespaces}{#1}{#2}%
}%

\begin{document}

\section{TitleA/TitleB}
\section{\mulanr{TitleA}{TitleB} }

test


\end{document}

在此处输入图片描述

答案2

我已通过在命令定义中使用 \@bsphack \@esphack 解决了该问题。修改后的 mulan.sty 如下。

% Multi-language support package<mulan.sty>
% Copyright 2015~2015, JILONG YIN
% JILONG YIN <[email protected]>
% The Current Maintainer of this work is JILONG YIN.
% 
% \mulan[en,jp,cn,...]{seperator}{before}{after}{en-sentence}{jp-sentence}{cn-sentence}
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mulan}[2015/02/25 Multi-language support package version 0.0.1]
%
% Brand marks, and version info
\def\meta@version{0.0.2}
\def\metaversion{\meta@version}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Multi-language mixed document supported commands
% in default, all of the language is off.
% Using the following commands to turn on/off the specified language.
%  \entrue \enfalse \cntrue \cnfalse \jptrue \jpfalse \rutrue \rufalse
%
% Specify default language sequence,
% usage:
%  \mulanseq{en,jp,cn}
%  if it is not specified, the language sequence is {en,jp,cn}
%
% usage: 
%  \mulan[en,jp,cn,...]{seperator}{before}{after}{en-sentence}{jp-sentence}{cn-sentence}
%
% if the language sequence is not given, 
% given global language sequence \mulanseq will be used  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{xeCJK}
\setCJKfamilyfont{zhrm}{SimSun} %Chinese
\setCJKfamilyfont{jarm}{IPAMincho} %Japanese
\setCJKfamilyfont{korm}{Batang} %Korean
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\Chinese{\CJKfamily{zhrm}\CJKnospace}%
\newcommand\Japanese{\CJKfamily{jarm}\CJKnospace}%
\newcommand\Korean{\CJKfamily{korm}\CJKnospace}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifen \newif\iffr \newif\ifru \newif\ifjp \newif\ifcn \newif\ifkr
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\en#1{\ifen\callsep#1\fi\langcmd}%
\def\fr#1{\iffr\callsep#1\fi\langcmd}%
\def\ru#1{\ifru\callsep#1\fi\langcmd}%
\def\jp#1{\ifjp\callsep{\Japanese#1}\fi\langcmd}%
\def\cn#1{\ifcn\callsep{\Chinese#1}\fi\langcmd}%
\def\kr#1{\ifkr\callsep{\Korean#1}\fi\langcmd}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%specify the multi languge sequence variable globaly
\def\mulan@seq{en,jp,cn}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{mulan@args}%
\newcounter{mulan@argsmax}

%\newcommand\mulan[4][\mulan@seq]{%
\newcommand\mulan[4][en,jp,cn]{% 
\gdef\mulan@thesep{#2}% 
\gdef\mulan@thebefore{#3}% 
\gdef\mulan@theafter{#4}%
\setcounter{mulan@args}{0}%
\setcounter{mulan@argsmax}{0}%
\commaparse#1,\relax%
\stepcounter{mulan@args}%
\expandafter\def\csname arg\romannumeral\value{mulan@args}\endcsname{relax}%    
\setcounter{mulan@args}{0}% 
\@bsphack\mulan@thebefore\@esphack%
\langcmd%
}

\def\commaparse#1,#2\relax{%
\stepcounter{mulan@args}% 
\@bsphack\ifnum\value{mulan@args}>\value{mulan@argsmax}\setcounter{mulan@argsmax}{\value{mulan@args}}\fi\@esphack%get the max number of arguments is
\expandafter\def\csname arg\romannumeral\value{mulan@args}\endcsname{#1}%
\if\relax#2\else\commaparse#2\relax\fi%
}

\newcommand\langcmd{%
\stepcounter{mulan@args}% \ifnum\value{mulan@argsmax}<\value{mulan@args}\mulan@theafter\fi%
\csname\csname arg\romannumeral\value{mulan@args}\endcsname\endcsname%
}

\newcommand\callsep{\ifnum\value{mulan@args}>1\mulan@thesep\fi}

%%%%=======================================================
\def\mulanp{\expandafter\mulan{\par}{}{}}
\def\mulanr{\expandafter\mulan{\space{\textbf{/}}\space}{}{}}

\def\mulanTL{ \mulan{\\}{\begin{tabular}{l}}{\end{tabular}} }
\def\mulanTC{ \mulan{\\}{\begin{tabular}{c}}{\end{tabular}} }
\def\mulanTR{ \mulan{\\}{\begin{tabular}{r}}{\end{tabular}} }

相关内容