3

I have a problem about verbatim and listings. I need write some codes by using Thai font but it shows in picture

\documentclass[12pt,a4paper,oneside]{book}
\usepackage{verbatim}
\usepackage{listings}
\usepackage{xltxtra}
\usepackage{fontspec}
\usepackage{xunicode}
\defaultfontfeatures{Mapping=tex-text}
\XeTeXlinebreaklocale "th_TH" 
\XeTeXlinebreakpenalty=100
\XeTeXlinebreakskip=0pt plus 1pt
\setmainfont[Scale=1.4]{TH SarabunPSK} % Thai Font
\begin{document}
\begin{verbatim}
\begin{enumerate} 
\item one หนึ่ง
\item two สอง
\end{enumerate}     
\end{verbatim}  
\begin{lstlisting}
\begin{enumerate}
\item one หนึ่ง
\item two สอง
\end{enumerate}     
\end{lstlisting}
\end{document}

enter image description here

But I would like to get code like lstlisting but font like verbatim. How to improve it.

Mensch
  • 65,388
onpra7
  • 253
  • Because the Thai font you're using isn't installed on my machine, I cannot test this, but simply try to add \lstset{basicstyle=\ttfamily} to your preamble. – jub0bs Mar 23 '15 at 13:42
  • Thank you. I follow your advice, but Thai font does not appear. – onpra7 Mar 23 '15 at 13:56

2 Answers2

1

Here a solution I think it will succeed in your case (I have no Thai font), you can show Thai font using escapeinside which define two symbols between them you can insert your text.

Code

\documentclass[12pt,a4paper,oneside]{book}
\usepackage{verbatim}
\usepackage{listings}
\usepackage{xltxtra}
\usepackage{fontspec}
\usepackage{xunicode}
\defaultfontfeatures{Mapping=tex-text}
\XeTeXlinebreaklocale "th_TH" 
\XeTeXlinebreakpenalty=100
\XeTeXlinebreakskip=0pt plus 1pt
\setmainfont[Scale=1.4]{TH SarabunPSK} % Thai Font
\newfontfamily\thaifont{TH SarabunPSK}
\lstset{basicstyle=\ttfamily, escapeinside={*}{*}}
\begin{document}
\begin{verbatim}
\begin{enumerate} 
\item one หนึ่ง
\item two สอง
\end{enumerate}     
\end{verbatim}  
\begin{lstlisting}
\begin{enumerate}
\item one *\thaifont{หนึ่ง}*
\item two *\thaifont{สอง}*
\end{enumerate}     
\end{lstlisting}
\end{document}
Salim Bou
  • 17,021
  • 2
  • 31
  • 76
0

I suppose that there is no typewriter font for thai. You can try the Code2000 font, which is, of course, not a typewriter font, but has all characters:

\documentclass[12pt,a4paper,oneside]{book}
\usepackage{fontspec}
\setmainfont[Scale=1.4]{TH SarabunPSK} % Thai Font
\setmonofont[Scale=MatchLowercase]{Code2000}
\XeTeXlinebreaklocale "th_TH" 
\XeTeXlinebreakpenalty=100
\XeTeXlinebreakskip=0pt plus 1pt

\usepackage{verbatim}
\usepackage{listings}
\begin{document}

    \begin{verbatim}
    \begin{enumerate} 
    \item one หนึ่ง
    \item two สอง
    \end{enumerate}     
    \end{verbatim}  
    \begin{lstlisting}[basicstyle=\ttfamily\small]
    \begin{enumerate}
    \item one หนึ่ง
    \item two สอง
    \end{enumerate}     
    \end{lstlisting}
\end{document}

enter image description here

  • 1
    The lstlisting code will only work as long a the thai is on its own (starts with a space). bccสองccc would give wrong output สองbccccc. Then you need to declare the chars first,see http://tex.stackexchange.com/a/25396/2388. – Ulrike Fischer Dec 15 '15 at 09:56
  • @UlrikeFischer: Follow your comment I experienced another problem of incorrect rendering (tested with Khmer). Any improvement? – Say OL Aug 11 '17 at 11:22