3

I am using \readline to paste the contents of a plain text file to a LaTeX document, which works fine. This is what the plain text file looks like:

This is the first line.
This is the second line.

However, when I use fontspec to specify a font for the text, I get undesired white boxes at the end of each line – see below

Example with strange white box at end of line

Question: How do I remove the white box?

Adding linebreaks, \par or similar in the .tex file doesn't solve the problem. This problem is not specific to the selected font, e.g., I also get it for Arial, Georgia and other fonts. If I remove the command \myfont, then the problem does not occur.

My code:

\documentclass[12pt]{article}
\usepackage{pgffor}
\usepackage{fontspec}

\makeatletter

\newread\myread
\newcount\linecnt
\openin\myread=some_list.txt
\@whilesw\unless\ifeof\myread\fi{%
    \advance\linecnt by \@ne
    \readline\myread t\expandafter o\csname description\number\linecnt\endcsname
}

\makeatother

\newfontfamily\myfont{Amiri}


\begin{document}
    \myfont
    \foreach[] \n in {1,...,2}{
        {\csname description\n\endcsname}\par%
    }

\end{document}
New contributor
mto_19 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • I added a different (and in my opinion) better implementation.
    – egreg
    Commented Jul 5 at 10:38

1 Answer 1

3

It's not fontspec. If you remove it, together with \newfontfamily and \myfont, you get, with pdflatex,

enter image description here

The reason is that the endlines are converted to the character having code 13, which in the standard setting is the ligature “fl”. The Amiri font has no character there and LuaLaTeX uses a box.

The reason is that \readline stores the line of text as characters of category code 12 (except spaces that retain catcode 10).

You need to read the file with \endlinechar set to –1.

\begin{filecontents*}{\jobname.txt}
This is the first line.
This is the second line.
\end{filecontents*}

\documentclass[12pt]{article}
\usepackage{pgffor}
%\usepackage{fontspec}

\makeatletter

\newread\myread
\newcount\linecnt
\openin\myread=\jobname.txt
\chardef\stored@endlinechar=\endlinechar
\endlinechar=-1
\@whilesw\unless\ifeof\myread\fi{%
    \advance\linecnt by \@ne
    \readline\myread t\expandafter o\csname description\number\linecnt\endcsname
}
\endlinechar=\stored@endlinechar
\makeatother

%\newfontfamily\myfont{Amiri}


\begin{document}
%    \myfont
    \foreach[] \n in {1,...,2}{
        {\csname description\n\endcsname}\par
    }

\end{document}

no endlinechar

Removing the comments and running LuaLaTeX, you get

enter image description here

Actually, I believe you should use \read and not \readline, so category codes are normal and control sequences are interpreted.

This is how I'd implement the thing.

\begin{filecontents*}{\jobname.txt}
This is the first line.
This is the second line.
\end{filecontents*}

\documentclass[12pt]{article}
\usepackage{pgffor}
\usepackage{fontspec}

\ExplSyntaxOn

\ior_new:N \g_mitoxix_lines_ior

\NewDocumentCommand{\storelines}{mm}
 {% #1 = symbolic name, #2 = file name
  \seq_clear_new:c { l__mitoxix_lines_#1_seq }
  \ior_open:Nn \g_mitoxix_lines_ior { #2 }
  \ior_map_inline:Nn \g_mitoxix_lines_ior
   {
    \seq_put_right:ce { l__mitoxix_lines_#1_seq } { \tl_trim_spaces:n { ##1 } }
   }
  \iow_close:N \g_mitoxix_lines_ior
 }

\NewExpandableDocumentCommand{\useline}{mm}
 {% #1 = symbolic name, #2 = index
  \seq_item:cn { l__mitoxix_lines_#1_seq } { #2 }
 }

\ExplSyntaxOff

\newfontfamily\myfont{Amiri}

\storelines{description}{\jobname.txt}

\begin{document}
\myfont

\foreach \n in {1,...,2}{
   \useline{description}{\n}\par
}

\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .