General\input{} appends a space

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
User avatar
neoamsterdam
Posts: 3
Joined: Tue Sep 28, 2010 2:26 am

\input{} appends a space

Post by neoamsterdam »

I've discovered (the hard way) that \input{} appends a space to whatever it reads in from file. This is a major problem as I rely heavily on \input{}.

Consider the following minimal complete example:

Code: Select all

\documentclass[12pt]{book}
\begin{document}
\immediate\write18{echo -n "this quote" > temp.tex}
Where does the space at the end of ``\input{temp}'' come from?
\end{document}
One would expect the result to yield "Where does the space at the end of “this quote” come from?". Instead, pdflatex -shell-escape code.tex produces "Where does the space at the end of “this quote ” come from?".

I have ruled out \write18{} and echo as suspect because (1) temp.tex does not contain the errant space and (2) other included files (which weren't created by way of \write18) suffer from the same problem. Either TeX, LaTeX, or pdfLaTeX is responsible for this.

The question is how to prevent this unwanted space from being added. Suggestions are welcome; solutions, doubly so.
Last edited by neoamsterdam on Wed Sep 29, 2010 4:25 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

\input{} appends a space

Post by frabjous »

It would appear that \input reads its input as complete lines (even when there's no linefeed at the end of the file). So in your case, it's as if you had written:

Code: Select all

Where does the space at the end of ``this quote
'' come from?
One way to see this is to to add a percent sign at the end of temp.tex. In other words, if temp.tex looks like this:

Code: Select all

this quote%
Then

Code: Select all

Where does the space at the end of ``\input{temp.tex}'' come from?
Is processed as if you had written:

Code: Select all

Where does the space at the end of ``this quote%
'' come from?
which actually produces the desired look, since the line ending is commented out.

Unfortunately, it's not obvious to me how to write a percent sign to an external file using \write18 in LaTeX. Maybe someone else reading this will know how to do it. Neither:

Code: Select all

\immediate\write18{echo -n "this quote%" > temp.tex}
nor

Code: Select all

\immediate\write18{echo -n "this quote\%" > temp.tex}
work. (The former is treated as commenting out the rest of the line, and the latter puts the slash into temp.tex as well.)

My temporary kludge has been to write a simple bash script addpercent.sh which does nothing put echo a '%':

Code: Select all

#!/bin/bash
echo -n '%'
exit
And then:

Code: Select all

\documentclass[12pt]{book}
\begin{document}
    \immediate\write18{echo -n "this quote" > temp.tex}
    \immediate\write18{addpercent.sh >> temp.tex}
    There is now no space at the end of ``\input{temp}''. Good.
\end{document}
Gives:
input.png
input.png (3.15 KiB) Viewed 2262 times
This, however, is an ugly hack all around, and it would be great if someone knew a more elegant solution. I think in your case, however, there are probably more elegant ways to achieve what you want, but without knowing more about how you're using \input{}, and why, it's hard to know.
User avatar
neoamsterdam
Posts: 3
Joined: Tue Sep 28, 2010 2:26 am

\input{} appends a space

Post by neoamsterdam »

One way to see this is to to add a percent sign at the end of temp.tex. In other words, if temp.tex looks like this:

Code: Select all

this quote%
That has indeed done the trick. Thank you, frabjous: I would not have thought of that.
Unfortunately, it's not obvious to me how to write a percent sign to an external file using \write18 in LaTeX. Maybe someone else reading this will know how to do it. Neither:

Code: Select all

\immediate\write18{echo -n "this quote%" > temp.tex}
nor

Code: Select all

\immediate\write18{echo -n "this quote\%" > temp.tex}
work. (The former is treated as commenting out the rest of the line, and the latter puts the slash into temp.tex as well.)
Indeed, I've come across that problem involving the use of date. For example:

Code: Select all

date +"%Y-%m-%dT%H:%M:%S%z"
will cause pdfLaTeX to choke, while...

Code: Select all

date +"\%Y-\%m-\%dT\%H:\%M:\%S\%z"
...will return "\2010-\09-\28T\05:\48:\40\+0000".

The workaround I employ is to call a shell script through \write18 and pass the data to be processed as an argument instead. This method bypasses the problem. Here's how I am decoding UNIX timestamps in hex for use in my document (now including the "append a % at the end" solution):

Code: Select all

\newcommand{\UNIXtoDate}[1]{\immediate\write18{./UNIXtoDate.sh #1 > temp.tex}\input temp}
...where UNIXtoDate.sh consists of...

Code: Select all

date -r $(( 0x$1 )) +"%Y-%m-%dT%H:%M:%S%z%%"
So, even though it's guilty of being "an ugly hack all around", it works like a charm. Thank you, frabjous.
..there are probably more elegant ways to achieve what you want, but without knowing more about how you're using \input{}, and why, it's hard to know.
You are correct - there should be more elegant solutions. However, here are a few examples of why I've chosen this workflow:
  • Try as I might, numprint will not produce US-style numbers ("1 234 567,89" instead of "1,234,567.89"), nor will it automagically convert them into old-style numbers ("\textoneoldstyle\texttwooldstyle,\textthreeoldstyle\textfouroldstyle\textfiveoldstyle").
  • pst-barcode paradoxically expects you to know the size of the QR code before it's been produced, even though the dimensions of a QR code are the result of the data it's encoding.
  • nbaseprt will not pad digits to a power-of-two on its own (ex.: "$0F", "$0ABC", "$0000CAFE"), nor will it handle decimals (ex.: "$03.243F").
  • And there is no package (at least that I know of) that will create artwork on-the-fly as a function of \thepage in order to create a images along the edges of the pages (more akin to scanlines on a television than thumby).
In almost all of these cases, writing a shell script to get the result I want is far easier than butchering TeX in order to reimplement an existing *NIX tool.

Once again, thank you, frabjous.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

\input{} appends a space

Post by localghost »

If the problem is solved with that, please be so kind and mark the topic accordingly as clear written in Section 3 of the Board Rules (to be read before posting). Otherwise tell us what is missing. Please keep that in mind for the future so that a reminder will not be necessary anymore.


Best regards and welcome to the board
Thorsten
User avatar
neoamsterdam
Posts: 3
Joined: Tue Sep 28, 2010 2:26 am

Re: [Solved] \input{} appends a space

Post by neoamsterdam »

Apologies, localghost. And the worst part is I remember having read that! :roll:
Post Reply