2

I am writing a file in Hebrew, and was trying to give a code example from a lecture in it. But each time I switch to LyX-Code Environment (and the English language), and type in a number and a symbol after a space, it changes their order when compiling.

For example, what I want:

return 0;

What I get:

return ;0

Note 1: I am not using Insert -> Program Listing, because for some reason it is messed up because of the file being in Hebrew.

Note 2: This problem only happens when numbers AND symbols are involved after a space (as seen in the example), if a letter is involved it won't happen.

Note 3: This problem only appear in the PDF after compiling, in the Editor itself it looks the same as I want it to Look.

Note 4: Each time I type some sort of a bracket in a line alone, the compiler would switch it direction. From } to {. I assume this problem is caused from the same reason, but I will note it anyway.

EDIT: I have been asked to post a LaTeX output exported from LyX:

%% LyX 2.3.1 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english,hebrew]{article}
\usepackage{fontspec}
\setmainfont[Ligatures=TeX]{Times New Roman}
\setsansfont[Ligatures=TeX]{Arial}
\setmonofont{Courier New}
\setlength{\parindent}{0bp}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
\providecommand\textquotedblplain{%
  \bgroup\addfontfeatures{RawFeature=-tlig}\char34\egroup}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\newenvironment{lyxcode}
    {\par\begin{list}{}{
        \setlength{\rightmargin}{\leftmargin}
        \setlength{\listparindent}{0pt}% needed for AMS classes
        \raggedright
        \setlength{\itemsep}{0pt}
        \setlength{\parsep}{0pt}
        \normalfont\ttfamily}%
     \item[]}
    {\end{list}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage[left]{lineno}
\linenumbers

\makeatother

\usepackage{polyglossia}
\setdefaultlanguage{hebrew}
\setotherlanguage{english}
\begin{document}
\begin{english}%
\begin{lyxcode}
\#include<stdio.h>

int~a~=~0;

int~main(void)

\{
\begin{lyxcode}
while~(a~<~10)

\{
\begin{lyxcode}
printf(\textquotedbl\%d\textquotedbl ,~a);

a~+=~1;
\end{lyxcode}
\}

return~0;
\end{lyxcode}
\}

\begin{hebrew}%
\end{hebrew}%
\end{lyxcode}
\end{english}

\end{document}

Thank you for your help.

EDIT 2: This preamble code \let\hebrewfonttt\ttfamily seems to fix the problem.

  • Welcome to TeX.SX! This sounds more like a bug report, which would be off-topic on this site. – TeXnician Sep 21 '18 at 10:27
  • @TeXnician Oh. Sorry. Would you mind pointing me in the direction of where I should report the bug? And should I delete the post afterwards? – amam3214 Sep 21 '18 at 10:32
  • 2
    Link to the bug tracker: lyx.org/trac/wiki/BugTrackerHome. I would not delete the post but wait what others say (maybe I'm just wrong that this is off-topic). – TeXnician Sep 21 '18 at 10:34
  • I doubt this is a LyX bug. Can you edit your question and post the LaTeX output exported from LyX? – David Purton Sep 22 '18 at 12:35
  • I can't manage to successfully compile your MWE (even with your suggested fix). But inserting code in this way seems like a really bad idea. I would try and get things working properly as they should be first. I can get a minimal Hebrew document with English code listings working OK in LyX. – David Purton Sep 23 '18 at 11:58
1

Update

Oh! I got it. The problem is that Times New Roman, Arial, and Courier don't support Hebrew Script (just glyphs).

It's enough just to define Hebrew fonts in the preamble.

Then just set your LyX code listing up as normal and it should all work.

Note

This still has some problems (I managed to trip some bracket problems in the code listing in certain circumstances.). If you use fonts that properly support both Hebrew and English (e.g., FreeSerif, FreeSans, FreeMono), you don't need to put anything in the preamble. You just need to specify basicstyle=\ttfamilylatin for the code listing options.

Anyway, with you current font selection, preamble should look like this:

\newfontfamily\hebrewfont{Times New Roman}[Ligatures=TeX]
\newfontfamily\hebrewfontsf{Arial}[Ligatures=TeX]
\newfontfamily\hebrewfonttt{Courier New}

MWE

#LyX 2.3 created this file. For more info see http://www.lyx.org/
\lyxformat 544
\begin_document
\begin_header
\save_transient_properties true
\origin unavailable
\textclass article
\begin_preamble
\newfontfamily\hebrewfont{Times New Roman}[Ligatures=TeX]
\newfontfamily\hebrewfontsf{Arial}[Ligatures=TeX]
\newfontfamily\hebrewfonttt{Courier New}
\end_preamble
\use_default_options true
\maintain_unincluded_children false
\language hebrew
\language_package auto
\inputencoding auto
\fontencoding global
\font_roman "default" "Times New Roman"
\font_sans "default" "Arial"
\font_typewriter "default" "Courier New"
\font_math "auto" "auto"
\font_default_family default
\use_non_tex_fonts true
\font_sc false
\font_osf false
\font_sf_scale 100 100
\font_tt_scale 100 100
\use_microtype false
\use_dash_ligatures true
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_package amsmath 1
\use_package amssymb 1
\use_package cancel 1
\use_package esint 1
\use_package mathdots 1
\use_package mathtools 1
\use_package mhchem 1
\use_package stackrel 1
\use_package stmaryrd 1
\use_package undertilde 1
\cite_engine basic
\cite_engine_type default
\biblio_style plain
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\justification true
\use_refstyle 1
\use_minted 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\is_math_indent 0
\math_numbering_side default
\quotes_style english
\dynamic_quotes 0
\papercolumns 1
\papersides 1
\paperpagestyle empty
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header

\begin_body

\begin_layout Standard
יברית
\end_layout

\begin_layout Standard

\lang english
\begin_inset listings
lstparams "language=C,numbers=left,tabsize=4,basicstyle={\ttfamily}"
inline false
status open

\begin_layout Plain Layout

#include <stdio.h>
\end_layout

\begin_layout Plain Layout

int a = 0;
\end_layout

\begin_layout Plain Layout

int main(void)
\end_layout

\begin_layout Plain Layout

{
\end_layout

\begin_layout Plain Layout

    while (a < 10)
\end_layout

\begin_layout Plain Layout

    {
\end_layout

\begin_layout Plain Layout

        printf("%d", a);
\end_layout

\begin_layout Plain Layout

        a += 1;
\end_layout

\begin_layout Plain Layout

    }
\end_layout

\begin_layout Plain Layout

    return 0;
\end_layout

\begin_layout Plain Layout

}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
יברית
\end_layout

\begin_layout Standard

\lang english
English
\end_layout

\end_body
\end_document

LyX view

enter image description here

PDF output

enter image description here


LaTeX export for comparison

This is what LyX gives when exporting to LaTeX (XeTeX). This compiles cleaning with xelatex on my system.

%% LyX 2.3.0 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\listfiles
\documentclass{article}
\usepackage{fontspec}
\setmainfont[Mapping=tex-text]{Times New Roman}
\setsansfont[Mapping=tex-text]{Arial}
\setmonofont{Courier New}
\pagestyle{empty}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
\providecommand\textquotedblplain{%
  \bgroup\addfontfeatures{Mapping=}\char34\egroup}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\newfontfamily\hebrewfont{Times New Roman}[Ligatures=TeX]
\newfontfamily\hebrewfontsf{Arial}[Ligatures=TeX]
\newfontfamily\hebrewfonttt{Courier New}

\makeatother

\usepackage{listings}
\usepackage{polyglossia}
\setdefaultlanguage{hebrew}
\setotherlanguage{english}
\addto\captionsenglish{\renewcommand{\lstlistingname}{Listing}}
\addto\captionshebrew{\renewcommand{\lstlistingname}{רישום קוד}}
\renewcommand{\lstlistingname}{רישום קוד}

\begin{document}
יברית

\begin{english}[variant=american]%
\begin{lstlisting}[language=C,numbers=left,tabsize=4,basicstyle={\ttfamily}]
#include <stdio.h>
int a = 0;
int main(void)
{
    while (a < 10)
    {
        printf("%d", a);
        a += 1;
    }
    return 0;
}
\end{lstlisting}

\end{english}%
יברית

\begin{english}[variant=american]%
English
\end{english}%

\end{document}

\listfiles output

And to help debug, here's the output when compiling with \listfiles at the top of the file:

 *File List*
 article.cls    2014/09/29 v1.4h Standard LaTeX document class
  size10.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
fontspec.sty    2018/07/30 vv2.6h Font selection for XeLaTeX and LuaLaTeX
  xparse.sty    2018-05-12 L3 Experimental document command parser
   expl3.sty    2018-06-14 L3 programming layer (loader) 
expl3-code.tex    2018-06-14 L3 programming layer 
l3xdvipdfmx.def    
fontspec-xetex.sty    2018/07/30 vv2.6h Font selection for XeLaTeX and LuaLaTeX

 fontenc.sty
   tuenc.def    2017/04/05 v2.0i Standard LaTeX file
fontspec.cfg
listings.sty    2015/06/04 1.6 (Carsten Heinz)
  keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
 lstmisc.sty    2015/06/04 1.6 (Carsten Heinz)
listings.cfg    2015/06/04 1.6 listings configuration
polyglossia.sty    2018/04/07 v1.43 Alternative to Babel for XeLaTeX and LuaLaT
eX
etoolbox.sty    2018/08/19 v2.5f e-TeX tools for LaTeX (JAW)
makecmds.sty    2009/09/03 v1.0a extra command making commands
 xkeyval.sty    2014/12/03 v2.7a package option processing (HA)
 xkeyval.tex    2014/12/03 v2.7a key=value parser (HA)
ifluatex.sty    2016/05/16 v1.4 Provides the ifluatex switch (HO)
 ifxetex.sty    2010/09/12 v0.6 Provides ifxetex conditional
gloss-hebrew.ldf    polyglossia: module for hebrew
hebrewcal.sty    2015/04/17 v2.6 Hebrew calendar for polyglossia (adapted from 
hebcal.sty in Babel)
    bidi.sty    2018/08/12 v33.1 Bidirectional typesetting in plain TeX and LaT
eX
   iftex.sty    2013/04/04 v0.2 Provides if(tex) conditional for PDFTeX, XeTeX,
 and LuaTeX
biditools.sty    2018/02/22 v1.4 Programming tools for bidi package
zref-abspage.sty    2016/05/21 v2.26 Module abspage for zref (HO)
zref-base.sty    2016/05/21 v2.26 Module base for zref (HO)
 ltxcmds.sty    2016/05/16 v1.23 LaTeX kernel commands for general use (HO)
infwarerr.sty    2016/05/16 v1.4 Providing info/warning/error messages (HO)
kvsetkeys.sty    2016/05/16 v1.17 Key value parser (HO)
etexcmds.sty    2016/05/16 v1.6 Avoid name clashes with e-TeX commands (HO)
kvdefinekeys.sty    2016/05/16 v1.4 Define keys (HO)
pdftexcmds.sty    2018/01/30 v0.27 Utility functions of pdfTeX for LuaTeX (HO)
   ifpdf.sty    2017/03/15 v3.2 Provides the ifpdf switch
 auxhook.sty    2016/05/16 v1.4 Hooks for auxiliary files (HO)
atbegshi.sty    2016/06/09 v1.18 At begin shipout hook (HO)
latex-xetex-bidi.def    2018/02/22 v1.3.3 bidi adaptations for `latex.ltx' for 
XeTeX engine
footnote-xetex-bidi.def    2017/08/09 v1.1 bidi footnote macros for XeTeX engin
e
article-xetex-bidi.def    2017/06/09 v0.6 bidi adaptations for standard article
 class for XeTeX engine
listings-xetex-bidi.def    2017/01/14 v1.4 bidi adaptations for listings packag
e for XeTeX engine
loadingorder-xetex-bidi.def    2017/12/29 v0.97 making sure that bidi is the la
st package loaded
cal-util.def
babel-hebrewalph.def    2010/03/02 Babel definitions for Hebrew numerals
 Adapted from hebrew.ldf (2005/03/30 v2.3h)
gloss-english.ldf    polyglossia: module for english
lstlang1.sty    2015/06/04 1.6 listings language file
 lstmisc.sty    2015/06/04 1.6 (Carsten Heinz)
 ***********
| improve this answer | |
  • I can't manage to import the LaTeX code you shared back into LyX (I am using LyX version 2.3.1). I tried adding the line of code \newfontfamily\englishfonttt{FreeMono}[Script=Latin] into the LaTeX preamble, as you said "The key is to define an English mono spaced font with Latin script in the preamble", but I still get the same error messages as seen in this post when trying to use Insert -> Program Listing – amam3214 Sep 23 '18 at 14:10
  • This is just a LyX file. Save it as something.lyx and see if you can open it. – David Purton Sep 23 '18 at 14:33
  • Copied the above code into notepad and tried to open it as a .lyx file. If this is what u meant, it didn't work. I also tried copying the code into a new LyX file's LaTeX preamble, which resulted in error messages upon compiling. – amam3214 Sep 23 '18 at 14:41
  • @amam3214, this is probably because you don't have the fonts I used in the example. I'll update the answer to use Times New Roman, Arial, and Courier New. You need a couple of extra lines in the preamble for these fonts to use Hebrew, because although they include glyphs, they don't support Hebrew script. – David Purton Sep 24 '18 at 0:00
  • I am sorry, but even after adding those three lines of code to the LaTeX Preamble, there is no effect and I still get these same error messages. – amam3214 Sep 24 '18 at 8:05

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.