1

In a document I'm writing in LyX I have three characters that are generating trouble: ♯, ♭ and ♮. This is how they are used in a program listing: enter image description here

and they generate this error:

enter image description here

and when I allow non-TeX fonts, this is the error:

enter image description here

I'm a complete beginner to LyX and Tex. What would be the appropriate solution here?

Here's a minimal example that's causing the error:

#LyX 2.2 created this file. For more info see http://www.lyx.org/
\lyxformat 508
\begin_document
\begin_header
\save_transient_properties true
\origin unavailable
\textclass scrbook
\use_default_options true
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman "default" "default"
\font_sans "default" "default"
\font_typewriter "default" "default"
\font_math "auto" "auto"
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100 100
\font_tt_scale 100 100
\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
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\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
The charachters outside a program listing seem to be fine:
\end_layout

\begin_layout Standard
♯
\end_layout

\begin_layout Standard
♭
\end_layout

\begin_layout Standard
♮
\end_layout

\begin_layout Standard
But in the program listing they don't work:
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

  public String symbol() {
\end_layout

\begin_layout Plain Layout

    switch (this) {
\end_layout

\begin_layout Plain Layout

      case Sharp:
\end_layout

\begin_layout Plain Layout

        return "♯";
\end_layout

\begin_layout Plain Layout

      case Flat:
\end_layout

\begin_layout Plain Layout

        return "♭";
\end_layout

\begin_layout Plain Layout

      case Natural:
\end_layout

\begin_layout Plain Layout

        return "♮";
\end_layout

\begin_layout Plain Layout

      case None:
\end_layout

\begin_layout Plain Layout

      default:
\end_layout

\begin_layout Plain Layout

        return "";
\end_layout

\begin_layout Plain Layout

    }
\end_layout

\begin_layout Plain Layout

  }
\end_layout

\end_inset


\end_layout

\end_body
\end_document
3
  • Can you post a minimal .lyx file to reproduce the issue? For more info, see wiki.lyx.org/FAQ/MinimalExample
    – scottkosty
    Commented Jul 30, 2016 at 5:01
  • @scottkosty I added it as text in here, is that ok, or should I upload the lyx file somewhere? Commented Jul 30, 2016 at 9:44
  • as you did was perfect. Nice MWE.
    – scottkosty
    Commented Jul 30, 2016 at 15:44

2 Answers 2

1

This solution is based on the solution here:

In LyX, first go to Document > Settings > Listings and put in there the following line:

escapeinside={\%*}{*)}

Then in LyX, go to View > Source. A panel will pop up at the bottom which shows the LaTeX code that LyX would export (remember that LyX creates PDF by exporting to LaTeX first). In the main text area of LyX, put the cursor next to ♯. In the source preview it will show

\ensuremath{\sharp}

Copy that and put it in-between the escape markers that we defined in the first step. Do the same for the others.

Here is the resulting document:

#LyX 2.2 created this file. For more info see http://www.lyx.org/
\lyxformat 508
\begin_document
\begin_header
\save_transient_properties true
\origin unavailable
\textclass scrbook
\use_default_options true
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman "default" "default"
\font_sans "default" "default"
\font_typewriter "default" "default"
\font_math "auto" "auto"
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100 100
\font_tt_scale 100 100
\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
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\listings_params "escapeinside={\%*}{*)}"
\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
The charachters outside a program listing seem to be fine:
\end_layout

\begin_layout Standard
♯
\end_layout

\begin_layout Standard
♭
\end_layout

\begin_layout Standard
♮
\end_layout

\begin_layout Standard
But in the program listing they don't work:
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

  public String symbol() {
\end_layout

\begin_layout Plain Layout

    switch (this) {
\end_layout

\begin_layout Plain Layout

      case Sharp:
\end_layout

\begin_layout Plain Layout

        return "%*
\backslash
ensuremath{
\backslash
sharp}*)";
\end_layout

\begin_layout Plain Layout

      case Flat:
\end_layout

\begin_layout Plain Layout

        return "%*
\backslash
ensuremath{
\backslash
flat}*)";
\end_layout

\begin_layout Plain Layout

      case Natural:
\end_layout

\begin_layout Plain Layout

        return "%*
\backslash
ensuremath{
\backslash
natural}*)";
\end_layout

\begin_layout Plain Layout

      case None:
\end_layout

\begin_layout Plain Layout

      default:
\end_layout

\begin_layout Plain Layout

        return "";
\end_layout

\begin_layout Plain Layout

    }
\end_layout

\begin_layout Plain Layout

  }
\end_layout

\end_inset


\end_layout

\end_body
\end_document

And here is the resulting PDF:

enter image description here

0

This answer uses non-TeX fonts and XeTeX:

Go to Document > Settings > Fonts and click "use non-TeX fonts". I chose the FreeSans fonts, which are common on my OS, Ubuntu:

enter image description here

Save, and then view with XeTeX. I get the following PDF:

enter image description here

You may also use LuaTeX, but depending on you TeX Live installation you might need to add \usepackage{luatex85} to Document > Settings > Preamble.

You must log in to answer this question.

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