7

I'm using TeXMaker to make a document in LaTeX. So I already made several listing blocks, and when some error occurred, I simply was making a new line, like this:

{ß}{{\ss}}1
{ü}{{\"u}}1
{ä}{{\"a}}1

And so on. But now I have trying in different ways to make this block like below, but no success. I think that symbols in beginning of each line (like ゚ω゚ノ') are broken my code, but I really need of them. Can you help me to use them? I need to use some package? I have tried japanese package and math package too, but no success.

I'm trying to make this listing:

enter image description here

Below is a real example:

\documentclass{article}
\usepackage{listings}
\begin{document}

\medskip
\begin{lstlisting}[caption=THIS IS OK!]
Name.prototype = {
  methodName: function(params){
    var doubleQuoteString = "some text";
    var singleQuoteString = 'some more text';
    // this is a comment
    if(this.confirmed != null
    && typeof(this.confirmed) == 
    Boolean && this.confirmed == true){
      document.createElement('h3');
      $('#system').append("This looks great");
      return false;
    } else {
      throw new Error;
    }
  }
}
\end{lstlisting}

\begin{lstlisting}[caption=BUT THIS IS NOT OK! why?]
||   ゚ω゚ノ = Variavel { valor: undefined }
||   _ = Variavel { valor: 3 }
||   ゚ー゚ = Variavel { valor: 4}
||   o = Variavel { valor: 3}
||   ゚Θ゚ = Variavel { valor: 1}
||   c = Variavel { valor: 0}
||   ゚Д゚ = Variavel {
  valor: 
   { '゚Θ゚': Variavel { valor: 'f'},
     '゚ω゚ノ': Variavel { valor: 'a'},
     '゚ー゚ノ': Variavel { valor: 'd'},
     '゚Д゚ノ': Variavel { valor: 'e'},
     c: Variavel { valor: 'c'},
     o: Variavel { valor: 'o'},
     _: Variavel { valor: [Function: Function]},
     return: Variavel { valor: '\\'},
     '゚Θ゚ノ': Variavel { valor: 'b'}}
\end{lstlisting}

\end{document}
GuTheR
  • 73

1 Answers1

8

There are several issues at play.

First, unless you tell it otherwise listings uses the main document font. Except the Latin Modern font family does not have the Greek, Cyrillic, and Japanese glyphs you are using, so nothing prints.

We could use a font that contains everything. But it's an odd combination that won't be that easy to find. Instead, I'll use Noto Mono as the main mono spaced font. This contains Greek and Cyrillic glyphs, but not the Japanese ones. However, it's possible to substitute these for ones in the Noto Sans Mono CJK JP font. We do this like this:

\usepackage{fontspec}
\usepackage{newunicodechar}
% use Noto Mono as fixed pitch font
\setmonofont{Noto Mono}
% set up fall back font containing the missing Japanese glyphs
\newfontfamily\fallbackfont{Noto Sans Mono CJK JP}
\DeclareTextFontCommand{\textfallback}{\fallbackfont}
\newunicodechar{ー}{\textfallback{ー}}
\newunicodechar{ノ}{\textfallback{ノ}}
\newunicodechar{゚}{\textfallback{゚}}

You should now see all the glyphs in the listing.

But if you look, you'll notice that the position of the Greek, Cyrillic, and Japanese characters is wrong. You have to tell listings about them, which is a pain. Do it like this:

% fix up positioning of non-ascii characters in listings
\makeatletter
\lst@InputCatcodes
\def\lst@DefEC{%
    \lst@CCECUse \lst@ProcessLetter
      ^^80^^81^^82^^83^^84^^85^^86^^87^^88^^89^^8a^^8b^^8c^^8d^^8e^^8f%
      ^^90^^91^^92^^93^^94^^95^^96^^97^^98^^99^^9a^^9b^^9c^^9d^^9e^^9f%
      ^^a0^^a1^^a2^^a3^^a4^^a5^^a6^^a7^^a8^^a9^^aa^^ab^^ac^^ad^^ae^^af%
      ^^b0^^b1^^b2^^b3^^b4^^b5^^b6^^b7^^b8^^b9^^ba^^bb^^bc^^bd^^be^^bf%
      ^^c0^^c1^^c2^^c3^^c4^^c5^^c6^^c7^^c8^^c9^^ca^^cb^^cc^^cd^^ce^^cf%
      ^^d0^^d1^^d2^^d3^^d4^^d5^^d6^^d7^^d8^^d9^^da^^db^^dc^^dd^^de^^df%
      ^^e0^^e1^^e2^^e3^^e4^^e5^^e6^^e7^^e8^^e9^^ea^^eb^^ec^^ed^^ee^^ef%
      ^^f0^^f1^^f2^^f3^^f4^^f5^^f6^^f7^^f8^^f9^^fa^^fb^^fc^^fd^^fe^^ff%
      %   Θ       ω
      ^^^^0398^^^^03c9%
      %   Д
      ^^^^0414%
      %   ー       ノ       ゚
      ^^^^ff70^^^^ff89^^^^ff9f%
      ^^00}
\lst@RestoreCatcodes
\makeatother

And finally it will work as expected.

It's also possible to use the minted package. You will still need the font substitution stuff, but won't have to mess around to get the non-ascii characters to print in the right place. The downside of minted is that you need to compile it with the -shell-escape flag and you have to have the python backend stuff installed.

Here's the full MWE:

\documentclass{article}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage{listings}
\usepackage{minted}
\pagestyle{empty}
% use Noto Mono as fixed pitch font
\setmonofont{Noto Mono}
% set up fall back font containing the missing Japanese glyphs
% needed for both listings and minted
\newfontfamily\fallbackfont{Noto Sans Mono CJK JP}
\DeclareTextFontCommand{\textfallback}{\fallbackfont}
\newunicodechar{ー}{\textfallback{ー}}
\newunicodechar{ノ}{\textfallback{ノ}}
\newunicodechar{゚}{\textfallback{゚}}
% fix up positioning of non-ascii characters in listings
% only needed for listings
\makeatletter
\lst@InputCatcodes
\def\lst@DefEC{%
    \lst@CCECUse \lst@ProcessLetter
      ^^80^^81^^82^^83^^84^^85^^86^^87^^88^^89^^8a^^8b^^8c^^8d^^8e^^8f%
      ^^90^^91^^92^^93^^94^^95^^96^^97^^98^^99^^9a^^9b^^9c^^9d^^9e^^9f%
      ^^a0^^a1^^a2^^a3^^a4^^a5^^a6^^a7^^a8^^a9^^aa^^ab^^ac^^ad^^ae^^af%
      ^^b0^^b1^^b2^^b3^^b4^^b5^^b6^^b7^^b8^^b9^^ba^^bb^^bc^^bd^^be^^bf%
      ^^c0^^c1^^c2^^c3^^c4^^c5^^c6^^c7^^c8^^c9^^ca^^cb^^cc^^cd^^ce^^cf%
      ^^d0^^d1^^d2^^d3^^d4^^d5^^d6^^d7^^d8^^d9^^da^^db^^dc^^dd^^de^^df%
      ^^e0^^e1^^e2^^e3^^e4^^e5^^e6^^e7^^e8^^e9^^ea^^eb^^ec^^ed^^ee^^ef%
      ^^f0^^f1^^f2^^f3^^f4^^f5^^f6^^f7^^f8^^f9^^fa^^fb^^fc^^fd^^fe^^ff%
      %   Θ       ω
      ^^^^0398^^^^03c9%
      %   Д
      ^^^^0414%
      %   ー       ノ       ゚
      ^^^^ff70^^^^ff89^^^^ff9f%
      ^^00}
\lst@RestoreCatcodes
\makeatother
\begin{document}

\section*{Using listings}

\lstset{basicstyle=\ttfamily}
\begin{lstlisting}
||   ゚ω゚ノ = Variavel { valor: undefined }
||   _ = Variavel { valor: 3 }
||   ゚ー゚ = Variavel { valor: 4}
||   o = Variavel { valor: 3}
||   ゚Θ゚ = Variavel { valor: 1}
||   c = Variavel { valor: 0}
||   ゚Д゚ = Variavel {
  valor: 
   { '゚Θ゚': Variavel { valor: 'f'},
     '゚ω゚ノ': Variavel { valor: 'a'},
     '゚ー゚ノ': Variavel { valor: 'd'},
     '゚Д゚ノ': Variavel { valor: 'e'},
     c: Variavel { valor: 'c'},
     o: Variavel { valor: 'o'},
     _: Variavel { valor: [Function: Function]},
     return: Variavel { valor: '\\'},
     '゚Θ゚ノ': Variavel { valor: 'b'}}
\end{lstlisting}

\section*{Using minted}

\begin{minted}[fontfamily=tt]{text}
||   ゚ω゚ノ = Variavel { valor: undefined }
||   _ = Variavel { valor: 3 }
||   ゚ー゚ = Variavel { valor: 4}
||   o = Variavel { valor: 3}
||   ゚Θ゚ = Variavel { valor: 1}
||   c = Variavel { valor: 0}
||   ゚Д゚ = Variavel {
  valor: 
   { '゚Θ゚': Variavel { valor: 'f'},
     '゚ω゚ノ': Variavel { valor: 'a'},
     '゚ー゚ノ': Variavel { valor: 'd'},
     '゚Д゚ノ': Variavel { valor: 'e'},
     c: Variavel { valor: 'c'},
     o: Variavel { valor: 'o'},
     _: Variavel { valor: [Function: Function]},
     return: Variavel { valor: '\\'},
     '゚Θ゚ノ': Variavel { valor: 'b'}}
\end{minted}
\end{document}

enter image description here

David Purton
  • 25,884