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

  • 1
    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
  • 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 0: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 0:23
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

  • 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
  • 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 4: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 4:17
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

  • 1
    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
  • 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 7: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 9:44
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.