0

I've made a function/command for use mid-sentence, but it adds spaces everywhere it's inserted. Is there a way to flag it so it doesn't make them?

Code:

\def \testvar{0}
\newcommand{\test} {
    \ifnum \testvar=1 {
        test0
    }
    \else {
        test1
    }
}

Inside document:

aaaa \test aaaa

Output:

aaaa    test1  aaaa

Wanted output:

aaaa test1 aaaa
  • 3
    Add a % to the end of each line in the definition. See https://tex.stackexchange.com/questions/7453/what-is-the-use-of-percent-signs-at-the-end-of-lines – Steven B. Segletes Feb 24 '20 at 20:09
  • 1
    you have 7 space tokens in your definition (in addition to the space token that terminates the number 1 in the ifnum test) – David Carlisle Feb 24 '20 at 20:11
  • 1
    Also use it like aaaa \test{} aaaa or aaaa \test\ aaaa or use xspace, – Phelype Oleinik Feb 24 '20 at 20:12
  • Adding % seems to work, but there are problems with this "solution":
    1. It makes me unable to fold the function - some functions cover the whole screen, I can't work like that
    2. It doesn't look like it's the intended way of doing this - I'd prefer to use the TeX components properly
    – Random Persson Feb 24 '20 at 20:55
  • What do you mean "fold the function"? Does it have to do with how the definition looks in your editor, or how the output looks in your document? – Steven B. Segletes Feb 24 '20 at 20:56
  • I mean clicking the arrow next to a block of code in brackets to hide the contents - most editors have that function. When adding % the arrow disappears on Overleaf. – Random Persson Feb 24 '20 at 20:58
  • I am not familiar with that "function". However, the only alternative to using % to break a definition into multiple lines without the introduction of space is to not break the definition into multiple lines and keep typing until you hit the }. The treatment of line-ends by TeX is quite well known and documented. The answer at the linked question does a good job of explaining it. – Steven B. Segletes Feb 24 '20 at 21:00
  • Here's an example of what commands I'm dealing with for easier understanding: https://pastebin.com/RWjeCxHV I will have to look into the details of how it works I guess. Thanks. – Random Persson Feb 24 '20 at 21:05
  • Sorry, I am unable to access the site from my office machine. – Steven B. Segletes Feb 24 '20 at 21:09
  • I would however, note one aspect of token absorption. When a line ends on a command, no spurious space is introduced. So, if you can structure your definition to end lines on, for example, \else, \ifnum, and \fi, no % is needed to suppress spurious spaces. – Steven B. Segletes Feb 24 '20 at 21:15
  • look at any tex code and you will see that you need to either not add linebreaks or comment them out of you do not want to add space, I do not know why you say that this is not the intended way of marking this up? – David Carlisle Feb 24 '20 at 21:22
  • in your pastebin you have lots of \ifnum #1=0{ the extra space is coming from the newline after { but in that case you do not want the { there at all they are just putting groups around your \paragraph command, which is not normally wanted, so you could delete the { which will also remove the spurious space – David Carlisle Feb 24 '20 at 21:24
  • as well as the ends of lines you have \refstepcounter{zadcounterA}SPACE{ so additional spaces in the output. – David Carlisle Feb 24 '20 at 21:27
  • It's just basic code formatting; looking at code with all white characters removed is an absolute nightmare that I would never subject others to. From what I'm seeing LaTeX isn't too friendly of a language, I'm having a hard time putting up with it, haha. – Random Persson Feb 24 '20 at 21:39
  • 2
    @RandomPersson tex markup is optimised for marking up documents and in natural language text white space is not ignorable fluff for pretty printing. I f y o u a d d sp ac e in t h e wron g pla ces it doesn't work. – David Carlisle Feb 24 '20 at 21:44

0 Answers0