4

I am trying to write a macro to input a set of small text files into a larger file, in lualatex, but seeing some variances how the text is inserted in the main file.

So far I prepared this:

\documentclass[11pt]{report}

\usepackage{amsthm}
\usepackage{polyglossia}

\setmainfont{STIX Two Text}

\usepackage[
            a4paper,
            margin=4cm,
            marginparwidth=50pt,
          ]{geometry}

\newtheoremstyle{problemstyle}% name of the style to be used
  {\topsep}% measure of space to leave above the theorem. E.g.: 3pt
  {\topsep}% measure of space to leave below the theorem. E.g.: 3pt
  {}% name of font to use in the body of the theorem
  {0pt}% measure of space to indent
  {\bfseries}% name of head font
  {}% punctuation between head and body
  {  }% space after theorem head; " " = normal interword space
  {\thmname{#1}\thmnumber{ #2}\textbf{\thmnote{ (#3)}}}

\theoremstyle{problemstyle}
\newtheorem{problema}{\textbf{Problem}}[section]

\newcommand{\bp}[1]{\begin{problema}[#1] \input{#1} \label{#1}\end{problema}}

\begin{document}

\bp{Fa87}%
\begin{problema}[Fa87]
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{problema}
\begin{problema}[Fa87]
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{problema}
\begin{problema}[Fa87]
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{problema}

\end{document}

ẁith the text of Fa87 to be exactly

Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.

all of it to be processed with lualatex. The variance can be seen here:

variance in the spacing

where the spacing after the entry that uses the command \bp is about twice as much as the spacing of all others, entered directly into the text.

Observe that the text introduced by the command is exactly the same as the one directly in the main file.

The problem seems to be related to the text getting closer to the end of the line, but I fail to gresp how to address it.

2
  • Of course I have, With that you get the error: ! Missing \endcsname inserted. <to be read again> relax l.31 \bp{Fa87} % Commented 8 hours ago
  • 2
    Off-topic: Loading the polyglossia package without a language choice doesn't do much good. I guess you're loading polyglossia for its side-effect of sorts, which is to load the fontspec package, thereby enabling the use of \setmainfont.. For coding clarity, I think it would be useful to replace \usepackage{polyglossia} with \usepackage{fontspec} or -- better yet -- \usepackage{unicode-math} since (a) unicode-math also loads fontspec automatically and (b) unicode-math enables the use of \setmathfont. Commented 8 hours ago

3 Answers 3

3

The problem seems to be related to the text getting closer to the end of the line, but I fail to gresp how to address it.

The spacing issue is caused by your choice of definition of \bp, which inserts unneeded whitespace after \input{#1}. I suggest you place \label{#1} before \input{#1} in the definition of \bp.

enter image description here

\documentclass[11pt]{report}
\begin{filecontents*}[overwrite]{FA87.tex}
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{filecontents*}

\usepackage{unicode-math} % better than '\usepackage{polyglossia}`
\setmainfont{Stix Two Text}
\setmathfont{Stix Two Math} % new

\usepackage[a4paper,margin=4cm]{geometry}

\usepackage{amsthm}
\newtheoremstyle{problemstyle}% name of the style to be used
  {\topsep}% measure of space to leave above theorem 
  {\topsep}% measure of space to leave below theorem
  {}% no special font style for body of theorem
  {0pt}% measure of space to indent
  {\bfseries}% header text style: bold
  {}% no punctuation between head and body
  {  }% normal interwordspace after theorem head
  {\thmname{#1}\thmnumber{ #2}\textbf{\thmnote{ (#3)}}}

\theoremstyle{problemstyle} % switch to 'problemstyle' style
\newtheorem{problema}{Problem}[section] % no need for '\textbf' wrapper

\newcommand{\bp}[1]{%
  \begin{problema}[#1]\label{thm:#1}\input{#1}\end{problema}}

\begin{document}
\setcounter{chapter}{1} % just for this example
\setcounter{section}{2}

\bp{Fa87}
\begin{problema}[Fa8a]\label{thm:8a}
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{problema}
\begin{problema}[Fa8b]\label{thm:8b}
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{problema}
\begin{problema}[Fa8c]\label{thm:8c}
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{problema}

\noindent
Cross-references to problems \ref{thm:Fa87}, \ref{thm:8a}, \ref{thm:8b}, and \ref{thm:8c}.

\end{document}
5
  • @cfr - Assuming the filename extension is .tex, Is there a significant difference between \input #1 and \input{#1} for the use case at hand? It doesn't look like it to me. But, for the sake of simplicity, I'll change \input #1 to \input{#1}. Commented 8 hours ago
  • yes. \input{#1} first checks if the file exists and issues a latex error if not. \input #1 invokes the primitive and issues a low level tex error if the file doesn't exist. I am not sure if there is anything else e.g. if logging is affected or file listings. Commented 7 hours ago
  • 1
    @cfr - Thanks for this explanation. I guess this distinction has never affected my own work so far as I do tend to take care to create the external files I wish load with \input. :-) Commented 7 hours ago
  • 1
    well, you could always add an option with a commented line labelled uncomment if infallible? Commented 7 hours ago
  • 1
    @cfr — We will certainly pursue this option when we start using a royal “we” in our proclamations. :-) Commented 7 hours ago
2

There is no difference between inputting the problem from a file and having the same content in the main document. The difference in output is caused by differences in the input.

The content of your file is followed by a space and a \label{} before the end of the environment. So we have

Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
 \label{Fa87}%
\end{problema}

which is very different from

Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{problema}

The newline becomes a space, which is followed by another space and the label. Two spaces become one in the output, but that one remains.

To see for yourself, try

\documentclass[11pt]{report}
\usepackage{amsthm}
\usepackage{polyglossia}
\setmainfont{STIX Two Text}
\usepackage[ a4paper, margin=4cm, marginparwidth=50pt, ]{geometry}
\newtheoremstyle{problemstyle}% name of the style to be used
  {\topsep}% measure of space to leave above the theorem. E.g.: 3pt
  {\topsep}% measure of space to leave below the theorem. E.g.: 3pt
  {}% name of font to use in the body of the theorem
  {0pt}% measure of space to indent
  {\bfseries}% name of head font
  {}% punctuation between head and body
  {  }% space after theorem head; " " = normal interword space
  {\thmname{#1}\thmnumber{ #2}\textbf{\thmnote{ (#3)}}}

\theoremstyle{problemstyle}
\newtheorem{problema}{\textbf{Problem}}[section]
\newcommand{\bp}[1]{%
  \begin{problema}[#1]
    \input{#1}\label{#1}%
  \end{problema}%
}

\begin{document}

\bp{Fa87}%
\unskip
\begin{problema}[Fa87]
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$. \label{Fa87}
\end{problema}
\begin{problema}[Fa87]
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{problema}
\begin{problema}[Fa87]
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{problema}

\end{document}

the proof is in the picture

To avoid this, use \unskip, for example:

\newcommand{\bp}[1]{%
  \begin{problema}[#1]
    \input{#1}\unskip\label{#1}%
  \end{problema}%
}

undo spacing

1
  • Not really… \end{problema} will emit \par so removing a trailing space, but here there's a space before \label which makes for two spaces in output. No need for \unskip: just move \label to its proper place, that is, just after \begin{problema}. Commented 3 hours ago
2

Let's examine your code:

\newcommand{\bp}[1]{\begin{problema}[#1] \input{#1} \label{#1}\end{problema}}

You have spaces around \input{#1} and they're the source of you problem. Not the one at the left, because after processing \begin{problema}[Fa87] TeX is not yet in LR-mode (waiting for possible \label commands).

It's the one after \input{Fa87} that does the damage. Consider the following example:

\begin{filecontents*}{\jobname-one}
Test
\end{filecontents*}
\begin{filecontents*}{\jobname-two}
Test%
\end{filecontents*}

\documentclass{article}

\begin{document}

AAA\input{\jobname-one}BBB

AAA\input{\jobname-one} BBB

AAA\input{\jobname-two}BBB

\end{document}

example input

The single line in \jobname-one.tex has a trailing endline, which is converted to a space, which you see in the first line of the output. If you leave a space after \input{\jobname-one}, you get two spaces (second line of the output), because TeX is well after the tokenization phase when consecutive spaces are merged into a single one.

If you look at \jobname-two, you see that protecting the endline with % removes the space (third line of the output).

Does this mean that you need % at the end of your input files? Not at all!

The code for \end{problema} first of all issues \par, which will do \unskip. But this won't do if you have \label! This command inserts a “whatsit” (because it instructs TeX to do a \write in the aux file) and so the \unskip implicit in \par is not able to go past the \label and your trailing space from the input file is still present.

Actually, \label tries to overcome this problem with a low level trick, but it's only able to remove a possible space before it, not the one coming from \input{Fa87}. So, even

\newcommand{\bp}[1]{\begin{problema}[#1] \input{#1}\label{#1}\end{problema}}

would not remove the issue.

Do you need \unskip? No. Just place \label where it belongs, namely just after \begin{problema}.

There are several reasons for this:

  1. \label will set both a number for \ref and a page number for \pageref and you want this page number to be where the problem begins, not where it ends;
  2. If you have another \label in the text of the problem, the final reference for your \label{Fa87} might be the wrong one.

Fixed code:

\newcommand{\bp}[1]{\begin{problema}[#1]\label{#1}\input{#1}\end{problema}}

Test:

\begin{filecontents*}{Fa87}
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{filecontents*}

\begin{filecontents*}{Fa88}
Prove $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{filecontents*}


\documentclass[11pt]{report}
\usepackage[
  a4paper,
  margin=4cm,
  marginparwidth=50pt,
]{geometry}

\usepackage{amsthm}
\usepackage{unicode-math}

\setmainfont{STIX Two Text}
\setmathfont{STIX Two Math}


\newtheoremstyle{problemstyle}% name of the style to be used
  {\topsep}% measure of space to leave above the theorem. E.g.: 3pt
  {\topsep}% measure of space to leave below the theorem. E.g.: 3pt
  {}% name of font to use in the body of the theorem
  {0pt}% measure of space to indent
  {\bfseries}% name of head font
  {}% punctuation between head and body
  {  }% space after theorem head; " " = normal interword space
  {\thmname{#1}\thmnumber{ #2}\textbf{\thmnote{ (#3)}}}

\theoremstyle{problemstyle}
\newtheorem{problema}{\textbf{Problem}}[section]

\newcommand{\bp}[1]{\begin{problema}[#1]\label{#1}\input{#1}\end{problema}}

\begin{document}

\bp{Fa88}

\begin{problema}[Fa88]
Prove $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{problema}

\bp{Fa87}

\begin{problema}[Fa87]
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{problema}

\begin{problema}[Fa87]
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{problema}

\begin{problema}[Fa87]
Prove that $\cos^p \theta \leq \cos p\theta$ for
$0 \leq \theta \leq \pi /2$ and $0 < p < 1$.
\end{problema}

\end{document}

output

Watch out for spurious spaces!

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.