2

Unfortunately the next question in the series ASCII text set in Greek script when using \usepackage[greek]{babel} / Missing characters in output due to `\renewcommand{\familydefault}{\sfdefault}`

When we have a MWE like:

\documentclass[twoside]{book}

\usepackage{fontspec}
\usepackage[greek]{babel}

\setmainfont{Libertinus Sans}

\begin{document}

00027         printf({"{}JUST A PRINT STATEMENT\(\backslash\)n"{}}); \par
00027         printf({"{}Just a print statement\(\backslash\)n"{}}); \par
00028         printf({"{}Λίστα Δοκιμαστικών\(\backslash\)n"{}}); \par

  \normalfont\ttfamily%
{00027         printf({"{}JUST A PRINT STATEMENT\(\backslash\)n"{}});}\par
{00027         printf({"{}Just a print statement\(\backslash\)n"{}});}\par
{00028         printf({"{}Λίστα Δοκιμαστικών\(\backslash\)n"{}});}\par
  \normalfont%
  \normalsize%

\end{document}

The output is like:

enter image description here

we see that in the Momospaced part a number of Greek characters are missing, how to fix this?

Edit I used the advice from @Mico to use \setmonofont{Noto Mono}[Scale=MatchLowercase] (I actually copied his entire code), but got a strange output:

enter image description here

  • 3
    You need to use \setmonofont{<font>} with a font that supports Greek. – egreg Jun 11 at 12:10
  • Thanks for the suggestion. When I place \setmonofont{Libertinus Mono} just beneath \setmainfont{Libertinus Sans} I get all square boxes with a question mark in it (so character not found) in the last monospaced line. Any suggestion for a font / how I should improve my code? – albert Jun 11 at 12:19
  • 1
    That's because Libertinus Mono doesn't feature the required greek letters. – Mico Jun 11 at 12:23
  • @Mico I just looked in the log file and see Missing character: There is no Λ in font Libertinus Mono Regular/OT:script=latn ;language=dflt;! for a number of characters, but no clue from my side. Any suggestions for a Monospaced font with Greek characters? – albert Jun 11 at 12:24
  • @albert - Please see the answer I just posted. I chose Noto Mono, but there have got to be other suitable monospaced fonts out there as well. – Mico Jun 11 at 12:27
3

A monospaced font that provides the required Greek characters is Noto Mono. There must be many other suitable monospaced fonts out there.

enter image description here

\documentclass[twoside]{book}
\usepackage{fontspec}
\usepackage[greek]{babel}
\setmainfont{Libertinus Sans}
\setmonofont{Noto Mono}[Scale=MatchLowercase]

\begin{document}
\obeylines % to avoid having to type all those \par directives...
00027         printf({"{}JUST A PRINT STATEMENT\(\backslash\)n"{}}); 
00027         printf({"{}Just a print statement\(\backslash\)n"{}}); 
00028         printf({"{}Λίστα Δοκιμαστικών\(\backslash\)n"{}}); 
\ttfamily
00027         printf({"{}JUST A PRINT STATEMENT\(\backslash\)n"{}});
00027         printf({"{}Just a print statement\(\backslash\)n"{}});
00028         printf({"{}Λίστα Δοκιμαστικών\(\backslash\)n"{}});
\end{document}
| improve this answer | |
  • Thanks for the suggestion, your output looks nice, my output looks horrible (please see edited question). – albert Jun 11 at 12:42
  • Possibly Scale=MatchUppercase is better. – egreg Jun 11 at 12:42
  • @egreg unfortunately same sort of output (I tried it beforehand). – albert Jun 11 at 12:44
  • Looks like \setmonofont{Courier New} solves my problem for this small problem (for other outputs we already use Courier). I'm going to test it in the larger code base. – albert Jun 11 at 13:01
  • 1
    The help I got from the forum is very valuable, gives me insights and pointers to formulate searches with other possibilities / additional information. – albert Jun 11 at 15:38
3

You need to declare a monospaced font that supports Greek. Since your document is in sans serif, a choice might be DejaVu Sans Mono, which is also included in TeX Live. Since monospaced fonts tend to look bigger than they are, I applied a scale factor; experiment until you find the right value, but look at the screen from a distance.

\documentclass[twoside]{book}

\usepackage{fontspec}
\usepackage[greek]{babel}

\setmainfont{Libertinus Sans}
\setmonofont{DejaVu Sans Mono}[Scale=0.8]

\begin{document}

00027         printf("JUST A PRINT STATEMENT\textbackslash n"); \par
00027         printf("Just a print statement\textbackslash n"); \par
00028         printf("Λίστα Δοκιμαστικών\textbackslash n"); \par

\begin{verbatim}
00027   printf("JUST A PRINT STATEMENT\n");
00027   printf("Just a print statement\n");
00028   printf("Λίστα Δοκιμαστικών\n");
\end{verbatim}

\end{document}

Also, rather than using complicated constructions, verbatim is handier for such objects.

enter image description here

| improve this answer | |
  • With the DejaVu font I cannot create the pdf file, getting some "Driver error" but no further information and the pdf is corrupt beyond repair. I just found \setmonofont{Courier New} that looks like to solve my problem for this small problem (for other outputs we already use Courier). I'm going to test it in the larger code base. – albert Jun 11 at 13:01
  • When using the \begin{verbatim} construct I get dvipdfmx:fatal: This font using the "seac" command for accented characters..., but anyway "verbatim" construct might be a good construct but seen the fact that it is in generated code, that is also used for other output languages, I'm a bit reluctant to change it from "ttfamily" to "verbatim" – albert Jun 11 at 13:17
2

The Computer Modern Unicode fonts are clones of Knuth’s Computer Modern, like Latin Modern is, that contain Greek. So you could use

\setmainfont{CMU Serif}
\setsansfont{CMU Sans Serif}
\setmonofont{CMU Typewriter Text}

A good tip for solving bugs like this is to add \tracinglostchars=2 to the top of the file. Then TeX will tell you if the font you selected does not contain a character you’re using. By default, it silently logs a warning message to the .log file.

| improve this answer | |
  • The CMU fonts works nicely. I got the impression from the answer that also "Latin Modern" would work, so I tried to set \setmainfont{Latin Modern Sans} and \setmonofont{Latin Modern Mono} but this resulted again in the missing characters (gave a nice opportunity to test the \tracinglostchars=2). – albert Jun 12 at 8:15

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.