3

I am typing a document in Vietnamese about C++, so of course there will be some code. I will use lstlisting environment as it is the best choice for code.

However, the \ttfamily (default monospace font) seems to be too wide for me. I want to use Consolas instead. However, inconsolata package does not work when there is this line in the preamble:

\usepackage[utf8]{vietnam}

or

\usepackage[vietnamese=nohyphenation]{hyphsubst}
\usepackage[vietnamese]{babel}

Without one of these, all Vietnamese letters (like or ) will disappear in the PDF, though inconsolata works.

I am using pdfLaTeX. I know that I can change to XeLaTeX to type in Consolas easily, but it seems to be a bit too complicated for me to convert a preamble from pdfLaTeX to XeLaTeX. I've tried four or five times without success.

Anyone can give me some idea? Thank you very much in advance!

Here is my shortened preamble if you can convert it to XeLaTeX or LuaLaTeX:

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{vietnam}
\usepackage[unicode]{hyperref}
\usepackage{inconsolata}
\usepackage{listings}
\lstset{basicstyle=\footnotesize\fontfamily{zi4}\selectfont,}
...

My full code can be found here.

  • Don't point to an external document. Copy a full small example here. – Ulrike Fischer Oct 31 '18 at 16:53
  • Just as a pointer for the utf8 characters (you refer to as "Vietnamese letters"): Have a look at the literate option to get them. – TeXnician Oct 31 '18 at 17:04
4

As you said you would be willing to try Xe-/LuaLaTeX, I have made an attempt to convert the part that you ask about to LuaLaTeX (involving a font change to the Palatino-like TeX Gyre Pagella).

listings may still handle your unicode sequences ("Vietnamese letters") badly, so you should use the literate option.

\documentclass[11pt,a4paper]{article}
\usepackage[vietnamese=nohyphenation]{hyphsubst}
\usepackage[vietnamese]{babel}
\usepackage[unicode]{hyperref}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry} 
\usepackage{amsmath} 
\usepackage{amsfonts}
\usepackage{amssymb} 
\usepackage{listings}
\usepackage{xcolor}
\usepackage{indentfirst}
\usepackage{marvosym} %For emoji
\usepackage{fontspec}
\usepackage{unicode-math}
\setmainfont{TeX Gyre Pagella}
\setsansfont{TeX Gyre Heros}
\setmonofont{Inconsolata}
\setmathfont{TeX Gyre Pagella Math}

\setlength{\parskip}{1ex}
\setlength{\parindent}{3ex}
\hypersetup{
    colorlinks=true,
    linkcolor=black,
    filecolor=black,      
    urlcolor=darkscarlet,
    citecolor=darkscarlet
}
\urlstyle{tt}
\definecolor{darkscarlet}{rgb}{0.34, 0.01, 0.1}
\definecolor{ultramarineblue}{rgb}{0.25, 0.4, 0.96}
\definecolor{alizarin}{rgb}{0.82, 0.1, 0.26}
\definecolor{cadmiumgreen}{rgb}{0.0, 0.42, 0.24}
\definecolor{desertsand}{rgb}{0.93, 0.79, 0.69}
\lstdefinestyle{mystyle}{
    backgroundcolor=\color{white},   
    commentstyle=\color{alizarin},
    keywordstyle=\color{ultramarineblue},
    stringstyle=\color{cadmiumgreen},
    basicstyle=\footnotesize\ttfamily,%\ttfamily,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2,
    language=C++,
    frame=single
}
\lstset{style=mystyle}
\pagestyle{plain}
\begin{document}
Một cái code đơn giản trong C++: (\textit{A simple code written in C++:})
\begin{lstlisting}
#include<bits/stdc++.h>
using namespace std;
int main(){
    cout<<"Hello World!";
}
\end{lstlisting}
\end{document}
4

If you plan to use Vietnamese characters in listings (for comments, I guess), I'd recommend using minted.

There are a few monospaced fonts that support the T5 (Vietnamese) encoding. Here are some examples, the last one is inconsolata that, however, lacks support for Vietnamese, so it's not apt for the purpose.

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{vietnam}
\usepackage{minted}
\usepackage{inconsolata}
\usepackage[unicode]{hyperref}

%%% this is only for the inconsolata example
\makeatletter
\define@key{FV}{fontencoding}%
  {\ifstrempty{#1}%
    {\let\FV@FontEncoding\relax}%
    {\ifstrequal{#1}{none}%
      {\let\FV@FontEncoding\relax}%
      {\def\FV@FontEncoding{\fontencoding{#1}}}}}
 \expandafter\def\expandafter\FV@SetupFont\expandafter{%
   \expandafter\FV@FontEncoding\FV@SetupFont}
 \fvset{fontencoding=none}
\makeatother
%%% end

%%% this is only for the txtt example
\makeatletter
\input{t5txtt.fd}
\DeclareFontShape{T5}{txtt}{m}{it}{<->ssub * txtt/m/sl}{}
\makeatother
%%% end

\begin{document}

\subsubsection*{TXTT}

\setminted{fontfamily=txtt}

\begin{minted}{cpp}
#include<bits/stdc++.h>
using namespace std;
int main(){
    cout<<"Hello World!";
}
// Một cái code đơn giản trong C++
\end{minted}

\subsubsection*{LMTT}

\setminted{fontfamily=lmtt}

\begin{minted}{cpp}
#include<bits/stdc++.h>
using namespace std;
int main(){
    cout<<"Hello World!";
}
// Một cái code đơn giản trong C++
\end{minted}

\subsubsection*{QCR}

\setminted{fontfamily=qcr}

\begin{minted}{cpp}
#include<bits/stdc++.h>
using namespace std;
int main(){
    cout<<"Hello World!";
}
// Một cái code đơn giản trong C++
\end{minted}

\subsubsection*{ZLMTT}

\setminted{fontfamily=zlmtt}

\begin{minted}{cpp}
#include<bits/stdc++.h>
using namespace std;
int main(){
    cout<<"Hello World!";
}
// Một cái code đơn giản trong C++
\end{minted}

\subsubsection*{ZI4}

\fvset{fontencoding=T1}
\setminted{fontfamily=zi4}

\begin{minted}{cpp}
#include<bits/stdc++.h>
using namespace std;
int main(){
    cout<<"Hello World!";
}
\end{minted}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy