new command definition brings extra space

Sorry for my original example code can not relect exactly what I mean. I have made a new one.

I am trying to define a new command, but it seems some unwanted extra spaces are generated together. Could you let me know how to remove them?

The test code:

\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}


The package I used here is as the following.

% 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}} }


It is a liitle long and difficult to understand. Please refer to the thread also for understanding <Parse command argument separated by comma>

• What is the use case here? There are numerous ways of solving the problem, but it really depends on where this might end up being required. Using \unskip after #1 solves one problem. You also need % in your definition of \mulan. – Werner Feb 24 '15 at 8:00
• There are spurious spaces in the third and fourth line of the definition of \mulan – egreg Feb 24 '15 at 9:54
• Line 44 of our mulan.sty: remove space after \value{mulan@argsmax} and before \fi. Line 51: remove space before \mulan@theafter. Line 30 and 31: remove space at the end of line (shift the percent char left). Lines 18-23, 34, 39, 46, 51, 52, 55: the percent character is redundant here. Line 59: the double space before {\textbf{/}} and one \space after it should be removed. – wipet Feb 24 '15 at 10:32

I don't know why you want this, but...

\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}


• You are right. It removes the extra space. but sorry my example can not really reflect my meanings. I will give a more complexed example later. – Jilong Yin Feb 24 '15 at 8:05

I have managed to solve it by using \@bsphack \@esphack in command definition. The modified mulan.sty is as follows.

% Multi-language support package<mulan.sty>
% JILONG YIN <yinjilong@gmail.com>
% 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}} }

• What stray spaces were you intending to remove? Those surrounding the slash /? If so, removing the space before \space in the definition of \mulanr gets rid of one of those spaces. You have a number of stray spaces in your definitions, as other commenters have pointed out. – Steven B. Segletes Feb 25 '15 at 1:44
• Yes, I have found these points. Now I have removed rundant spaces in the program, and it works well. Once again for your continuous help. Your \mulan comand acts well now. – Jilong Yin Feb 25 '15 at 2:37