1

I am trying to print within a command, but after printing I see the text (command's argument) to be printed with changed length...

I wonder if there is a reason of that and how can I avoid it.

I will try to answer my question... but probably I will not give the best answer... So, If you want, you can try to answer...

MWE:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\setlength\parskip{10pt}
\setlength\parindent{0pt}

\newcommand{\myprint}[1]{
#1
}


\begin{document}



\section{In a section}

This is a text that will occupy  one full line and some more, I hope I can show it



\myprint{This is a text that will occupy  one full line and some more, I hope I can show it}

\end{document}

Output:

enter image description here

TH.
  • 62,639
koleygr
  • 20,105
  • 1
    There are two space tokens in your \myprint macro. Put % at the end of the two lines. – TH. Jun 27 '17 at 00:14
  • I don't want to change the first line... I just want to have the same output from my command and from the actual text in the body... I did not understood about the 'space tokens'... I will google it... – koleygr Jun 27 '17 at 00:17
  • There's a space after the { and one after the #1 from the new lines. You need to add a comment character after both to prevent that. – TH. Jun 27 '17 at 00:18
  • See https://tex.stackexchange.com/a/7459/647 for how that works. – TH. Jun 27 '17 at 00:23
  • 1
    … or write the definition \newcommand{\myprint}[1]{#1} all on the same line. Incidentally, in your example the first space actually has no effect because occurs in vertical mode; it will not be so in general. – GuM Jun 27 '17 at 00:23
  • 2
    In other words: change your command to \newcommand{\myprint}[1]{#1} or something equivalent. What you have now is equivalent to \newcommand{\myprint}[1]{ #1 } which has more spaces. – ShreevatsaR Jun 27 '17 at 00:23
  • I just tested \newcommand{\myprint}[1]{ %#1%} and it works fine... Thank you both... It is not exactly a dupplicate.. but can consider to be... If someone of you want to write in an answer what have already said I will accept it as answer (It was in more lines... can not show it here) – koleygr Jun 27 '17 at 00:28

2 Answers2

2

Edit: Long answer to understand what happens

(For shorter answer look at @Henri Menke's answer)

Many LaTeX users are also programmers, and being a LaTeX user you become somehow a kind of programmer. Coding has some rules on styling our structure and so writing a C program we may choose between the following styles:

1.

#include <stdio.h>

int main(void)
{
  int i=0;
  for (i=0;i<10;i++)
  {
     printf("Loop Count i=%d",i);
  }
     return 0;
}

2.

#include <stdio.h>

int main(void){
   int i=0;
   for (i=0;i<10;i++){
      printf("Loop Count i=%d",i);
   }
   return 0;
}

or 3.

#include <stdio.h>

int main(void){
   int i=0;
   for (i=0;i<10;i++){printf("Loop Count i=%d",i);}
   return 0;
}

In C programming the result is exactly the same but something similar in LaTeX coding will not have the same results.

Empty lines

One reason is that in LaTeX coding, an empty line is equivalent to a \par command and inserts a paragraph. So the next example will generate the following output:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{parskip}

\setlength{\parindent}{8pt}

\setlength{\parskip}{15pt}
\newcommand{\myprint}[1]{

#1

}


\begin{document}



\section{In a section}

First line in first section

\def\mymaintext{This is my  text that will occupy a big part of remaining line.}

Test before.\mymaintext
Test line after.

Test before.\myprint{\mymaintext}
Test line after.
\end{document}

enter image description here

Spaces

By removing the empty lines we remove the \par commands from our print command but we don't remove the spaces:

\newcommand{\myprint}[1]{
#1
}

The above command will generate an extra space before our argument (we will have one space there) and an extra space after (two spaces after).

The following changes are equivalent:

\newcommand{\myprint}[1]{#1}

or

\newcommand{\myprint}[1]{%
#1%
}

and we have removed the extra empty spaces.

For more look at the topic of space tokens that @TH. gave me in the comments of the question.


The following answer was my first answer before understanding exactly what happens and I'm leaving it here for someone who will read the comments

I don't really know why it happens, but I found that a solution is to print in a minipage of width=\linewidth.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\setlength\parskip{10pt}
\setlength\parindent{0pt}

\newcommand{\myprint}[1]{
\begin{minipage}{\linewidth}
#1
\end{minipage}
}


\begin{document}



\section{In a section}

This is a text that will occupy  one full line and some more, I hope I can show it



\myprint{This is a text that will occupy  one full line and some more, I hope I can show it}

\end{document}

Output:

enter image description here

koleygr
  • 20,105
  • 1
    Almost every time someone suggests using a minipage, it is unnecessary. This is no exception. – TH. Jun 27 '17 at 00:21
  • lol... minipages have saved me from many problems... And many noobs too :P @TH does minipage affects the rest of my text.. like space between paragraphs etc? – koleygr Jun 27 '17 at 00:31
  • No. It's just rarely the right solution. Sometimes it is, but less often than I see it used. – TH. Jun 27 '17 at 00:35
  • Thanks @TH I am going to use it with paragraphs as arguments.. So I think for my use it is a solution... thanks again! – koleygr Jun 27 '17 at 00:41
  • You should most definitely change \end{minipage} to \end{minipage}% -- unless you really want to generate a (in my view entirely spurious) additional \parskip at the end of the myprint environment... – Mico Jun 27 '17 at 05:29
  • 1
    Your code is wrong. Add some text after the \myprint line to see the problems. Or remove the \setlength\parindent{0pt}. And check the log-file: you get underfull warnings there. – Ulrike Fischer Jun 27 '17 at 07:07
  • 1
    This answer is completely incorrect, sorry. The error in the question is two spurious space tokens and the revised code here has four spurious space tokens and introduces an unnecessary box which will prevent page breaking and would produce unwanted indention if parskip wans't zero. – David Carlisle Jun 27 '17 at 09:44
  • Good job trying to understand what happens! I think the part about blank lines (and \par) is less relevant, and this (revised) answer would be better if you put the main part first (about your original code in the question, which only introduces spaces), and only later (if you want) the part with a blank line (which introduces a \par). Cheers, – ShreevatsaR Jun 28 '17 at 00:48
  • @ShreevatsaR I just tried to do the changes you said... but I am losing the point of all the answer... Empty lines have more obvious results than the spaces and helps to get to the point of the space tokens. So, thanks for the advice but I think it is more useful like this. (At least for them who knows as little as I did.) – koleygr Jun 28 '17 at 15:14
  • @koleygr Sure, you are the best judge on what is more informative to you. I made some formatting changes to your answer, feel free to revert if you think they don't help. By the way the answer is still wrong, because you say “The space in the end of our command is created by the closing bracket '}' that always leave a space to separate the enclosed variable (or text) from the following text” and this is not true at all. (In the place you use the macro, you have a line break between \myprint{\mymaintext} and Test line after and that's a space; the closing } never introduces a space.) – ShreevatsaR Jun 28 '17 at 18:46
  • Thank you very much @ShreevatsaR ... You are right that I had made a mistake there... I fixed that. Thanks... – koleygr Jun 28 '17 at 19:48
  • 1
    Looks correct now, upvoted :-) I appreciate you trying to understand, and trying to make it informative for others. – ShreevatsaR Jun 28 '17 at 19:56
2

Remove the spurious spaces (marked with <--). I also removed your bogus parameters for \parskip and \parindent. If you don't want to indent a single paragraph, use \noindent. If you do not want to indent every paragraph, use the parskip package but this will make typographers' eyes bleed.

\documentclass{article}
\usepackage[utf8]{inputenc}

\newcommand{\myprint}[1]{% <--
#1% <--
}

\begin{document}

\noindent This is a text that will occupy  one full line and some more, I hope I can show it

\noindent\myprint{This is a text that will occupy  one full line and some more, I hope I can show it}

\end{document}

enter image description here

Henri Menke
  • 109,596
  • thanks @Henri Menke... I forgot the parskip and parident commands there.. I use parskip package and just removed it for the MWE... this is thwe answer of TH above... I already show it works fine... for cases that we don't use a paragraph as our variable this way is better than my minipage because can be used inline too – koleygr Jun 27 '17 at 01:44
  • @koleygr, I think you should ask about whatever your real question is. There's nothing here that would work differently with a paragraph. – TH. Jun 27 '17 at 03:30
  • @TH. if i use mh print command inline line text\myprint{2ndtext} the way of the minipage is different from yours... This is what I mentioned above... It is not only for me the question... May be other people find some thinks they need to know and this is why I made this general question and keeping it general... Thanks – koleygr Jun 27 '17 at 04:01
  • 1
    @koleygr, Perhaps I misunderstood. I simply meant that \myprint defined with \newcommand\myprint[1]{#1} doesn't have different behavior when its argument has a paragraph or not. It sounds like you want your \myprint macro to start a new paragraph. If that's the case, then you want \newcommand\myprint{\par#1}. But since you haven't said what you're trying to do, we can really only guess. – TH. Jun 27 '17 at 04:09
  • @TH. I have solved my problem .. but I will edit my ansewer to make it usefull for others... What we discussed in the comments is enough but I think it would be better to be available as a accepted answer to others... thanks... I will edit my answer and make it good including what you said – koleygr Jun 27 '17 at 04:17