# How to print a text from inside a command without affecting its width?

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:

• There are two space tokens in your \myprint macro. Put % at the end of the two lines. – TH. Jun 27 '17 at 0: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 0: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 0:18
• … 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 0:23

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}


• 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 1: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 3: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 4:01
• @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 4:17

Edit: Long answer to understand what happens

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}


### 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:

• Almost every time someone suggests using a minipage, it is unnecessary. This is no exception. – TH. Jun 27 '17 at 0: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 0:31
• 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 7:07
• 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 9:44
• 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