I'm using the devanagari package to typeset Hindi/Sanskrit alphabet in LaTeX. I have defined commands like \newcommand{\S}{{\dn saa}}, \newcommand{\R}{{\dn re}} etc. From a usage point of view, I would like to use these commands in the following way:

  1. \S\R should be formatted without space between the two characters: output without space
  2. \S \R should be formatted with space between the two characters: output with space

First one works just fine, but the second one doesn't. I know writing it as \S{} \R{} would do the job, but I'm trying to avoid writing the two extra braces. Similarly, writing it as \S\ \R\ is what I'm currently doing right now, but it would be ideal if there is a way to get read of the trailing backslashes. Furthermore, I have looked at using the xspace package, but it doesn't seem to work with the devanagari package.

A very basic working example is as follows:


\newcommand{\S}{{\dn saa}}
\newcommand{\R}{{\dn re}}

\S\R \\     % output expected as #1
\S \R \\    % output expected as #2

To run the above snippet:

  1. Save the file as sample.dn
  2. Run devnag sample.dn in the terminal, this generates a file sample.tex
  3. Run pdflatex sample.tex in the terminal to generate the PDF

Is there any way to format \S \R as shown in #2 above?

  • 1
    Just curious: why are you using the old devanagari package instead of using XeTeX / LuaTeX which give you access to any Devanagari font you like, and your input can be more straightforward too (just UTF-8 Devanagari in your file, instead of \dn with an input convention)? – ShreevatsaR Mar 16 '18 at 19:18
  • 4
    since the space following a command simply indicates the end of the command, it is dropped after that function has been performed. if you want a real space, you need to indicate it more clearly. several ways: \S\ \R, {\S} \R, \S{} \R. – barbara beeton Mar 16 '18 at 19:19
  • 1
    The fact that \S\R and \S \R are equivalent is very fundamental to TeX — when TeX encounters the backslash and then the S, it skips ahead past any spaces, and then encounters \R, exactly the same as if you'd typed \S\R. So you need to find an alternative solution. (BTW, see here for the direct Unicode way instead of \dn.) – ShreevatsaR Mar 16 '18 at 19:25
  • 1
    @ShreevatsaR, thanks for pointing out the XeTeX + UTF-8 Devanagari approach - will give it a try. – PrasannaDate Mar 16 '18 at 19:37

Browse other questions tagged or ask your own question.