2

I am using tcolorbox with Exercise and Solution in Vietnamese, but I can't compile this code. What am I doing wrong?

\documentclass[12pt,a4paper]{article}
\usepackage{fouriernc}
\usepackage[utf8]{vietnam}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage{tcolorbox}

\tcbuselibrary{listings}
\tcbset{texercisestyle/.style={arc=0.5mm, colframe=blue!25!yellow!90!white,
colback=blue!25!yellow!5!white, coltitle=blue!25!yellow!40!black,
fonttitle=\small\sffamily\bfseries, fontupper=\small, fontlower=\small,
listing options={style=tcblatex,texcsstyle=*\color{red!40!black}},
}}
\newtcolorbox[auto counter,number within=section,list inside=exam]{baitap}[2][]{%
texercisestyle,
listing file={solutions/texercise\thetcbcounter.tex},
label={exe:#2},
record={\string\processsol{solutions/texercise\thetcbcounter.tex}{#2}},
title={Bài tập \thetcbcounter\hfill\textbf{Lời giải ở trang}  \pageref{sol:#2}},
list text={Exercise with solution on page \pageref{sol:#2}},#1}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\begin{document}
\section{Thử máy}
Thử máy
\tcbset{texercisestyle/.style={arc=0.5mm, colframe=blue!25!yellow!90!white,
colback=blue!25!yellow!5!white, coltitle=blue!25!yellow!40!black,
fonttitle=\small\sffamily\bfseries, fontupper=\small, fontlower=\small,
listing options={style=tcblatex,texcsstyle=*\color{red!40!black}},
}}

\tcbstartrecording
\begin{baitap}{tabular_example}
Giải phương trình
\[(x^2 + 2x + 3) \cdot \sqrt{4x +5} + (6x^2 + 7x + 8) \cdot \sqrt{9x +10} = 9.\]
\begin{tcboutputlisting}
\begin{tcbwritetemp}
This is a solution.
\end{tcbwritetemp}
\end{tcboutputlisting}
\tcbuselistingtext%
\end{baitap}

\begin{baitap}{macro_twoparam}
\begin{tcboutputlisting}
Tôi không thể viết được tiếng Việt Nam (I can't write Vietnamese).
\end{tcboutputlisting}
\tcbuselistingtext%
This is a problem. \par\smallskip
\begin{tcbwritetemp}

\end{tcbwritetemp}
\tcbusetemplisting\par\smallskip\tcbusetemp%
\end{baitap}
\tcbstoprecording
\tcblistof[\subsection]{exam}{List of Exercises%
\label{listofexercises}}

\newtcbinputlisting{\processsol}[2]{%
texercisestyle,
listing only,
listing file={#1},
phantomlabel={sol:#2},%
title={Lời giải cho Bài tập \ref{exe:#2} ở trang \pageref{exe:#2}},
}
\tcbinputrecords
\end{document}

enter image description here

jub0bs
  • 58,916
minthao_2011
  • 4,534
  • 7
  • 36
  • 61

2 Answers2

3

Since VnTeX uses vnr font by default, if you want your listing to use the same font, you can add fontfamily=vnr to minted options. This works for me. enter image description here

% !TeX encoding=UTF-8
\documentclass[12pt,a4paper]{article}
\usepackage{fouriernc}
\usepackage[utf8]{vietnam}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage{tcolorbox}

\tcbuselibrary{minted}
\tcbset{texercisestyle/.style={arc=0.5mm, colframe=blue!25!yellow!90!white,
colback=blue!25!yellow!5!white, coltitle=blue!25!yellow!40!black,
fonttitle=\small\sffamily\bfseries, fontupper=\small, fontlower=\small,
%listing options={style=tcblatex,texcsstyle=*\color{red!40!black}},
minted options={fontfamily=vnr,fontsize=\small},
}}
\newtcolorbox[auto counter,number within=section,list inside=exam]{baitap}[2][]{%
texercisestyle,
listing file={solutions/texercise\thetcbcounter.tex},
label={exe:#2},
record={\string\processsol{solutions/texercise\thetcbcounter.tex}{#2}},
title={Bài tập \thetcbcounter\hfill\textbf{Lời giải ở trang}  \pageref{sol:#2}},
list text={Exercise with solution on page \pageref{sol:#2}},#1}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\begin{document}
\section{Thử máy}
Thử máy

\tcbstartrecording
\begin{baitap}{tabular_example}
Giải phương trình
\[(x^2 + 2x + 3) \cdot \sqrt{4x +5} + (6x^2 + 7x + 8) \cdot \sqrt{9x +10} = 9.\]
\begin{tcboutputlisting}
\begin{tcbwritetemp}
This is a solution.
\end{tcbwritetemp}
\end{tcboutputlisting}
\tcbuselistingtext%
\end{baitap}

\begin{baitap}{macro_twoparam}
\begin{tcboutputlisting}
   Bây giờ tôi có thể viết được tiếng Việt Nam (Now, I can write Vietnamese).
\end{tcboutputlisting}
\tcbuselistingtext%
My previous problem is solved. \par\smallskip
\begin{tcbwritetemp}

\end{tcbwritetemp}
\tcbusetemplisting\par\smallskip\tcbusetemp%
\end{baitap}
\tcbstoprecording
\tcblistof[\subsection]{exam}{List of Exercises%
\label{listofexercises}}

\newtcbinputlisting{\processsol}[2]{%
texercisestyle,
listing only,
listing file={#1},
phantomlabel={sol:#2},%
title={Lời giải cho Bài tập \ref{exe:#2} ở trang \pageref{exe:#2}},
}
\tcbinputrecords
\end{document}
2

This is a problem with the underlying listings package which gets problems when using utf8 characters. You could use \tcbuselibrary{listingsutf8} to relax the problem a little bit, because this maps the characters to a selectable 8-bit encoding (latin1 is the default). I do not know, if you can represent all Vietnamese characters in latin1, but I do not think so (it compiles, but the result is not good).

Another option would be to replace listings by minted. This allows using utf8 characters. Note that you may need to install additional software to use minted.

enter image description here

This is produced by:

% !TeX encoding=UTF-8
\documentclass[12pt,a4paper]{article}
\usepackage{fouriernc}
\usepackage[utf8]{vietnam}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage{tcolorbox}

\tcbuselibrary{minted}
\tcbset{texercisestyle/.style={arc=0.5mm, colframe=blue!25!yellow!90!white,
colback=blue!25!yellow!5!white, coltitle=blue!25!yellow!40!black,
fonttitle=\small\sffamily\bfseries, fontupper=\small, fontlower=\small,
%listing options={style=tcblatex,texcsstyle=*\color{red!40!black}},
minted options={fontsize=\small},
}}
\newtcolorbox[auto counter,number within=section,list inside=exam]{baitap}[2][]{%
texercisestyle,
listing file={solutions/texercise\thetcbcounter.tex},
label={exe:#2},
record={\string\processsol{solutions/texercise\thetcbcounter.tex}{#2}},
title={Bài tập \thetcbcounter\hfill\textbf{Lời giải ở trang}  \pageref{sol:#2}},
list text={Exercise with solution on page \pageref{sol:#2}},#1}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\begin{document}
\section{Thử máy}
Thử máy
\tcbset{texercisestyle/.style={arc=0.5mm, colframe=blue!25!yellow!90!white,
colback=blue!25!yellow!5!white, coltitle=blue!25!yellow!40!black,
fonttitle=\small\sffamily\bfseries, fontupper=\small, fontlower=\small,
%listing options={style=tcblatex,texcsstyle=*\color{red!40!black}},
minted options={fontsize=\small},
}}

\tcbstartrecording
\begin{baitap}{tabular_example}
Giải phương trình
\[(x^2 + 2x + 3) \cdot \sqrt{4x +5} + (6x^2 + 7x + 8) \cdot \sqrt{9x +10} = 9.\]
\begin{tcboutputlisting}
\begin{tcbwritetemp}
This is a solution.
\end{tcbwritetemp}
\end{tcboutputlisting}
\tcbuselistingtext%
\end{baitap}

\begin{baitap}{macro_twoparam}
\begin{tcboutputlisting}
Tôi không thể viết được tiếng Việt Nam (I can't write Vietnamese).
\end{tcboutputlisting}
\tcbuselistingtext%
This is a problem. \par\smallskip
\begin{tcbwritetemp}

\end{tcbwritetemp}
\tcbusetemplisting\par\smallskip\tcbusetemp%
\end{baitap}
\tcbstoprecording
\tcblistof[\subsection]{exam}{List of Exercises%
\label{listofexercises}}

\newtcbinputlisting{\processsol}[2]{%
texercisestyle,
listing only,
listing file={#1},
phantomlabel={sol:#2},%
title={Lời giải cho Bài tập \ref{exe:#2} ở trang \pageref{exe:#2}},
}
\tcbinputrecords
\end{document}

There may be other ways to use listings after all. For German umlauts, one can use

\lstset{literate=%
  {Ä}{{\"A}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
  {ä}{{\"a}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
  {ß}{{\ss}}1
}

A similar trick may work for Vietnamese characters too (?).

  • F.Sturm I copied your code. But I can not compile. I agree with you 'the result is not good'. – minthao_2011 Jun 30 '16 at 08:59
  • I hope, you add utf8 into your package. – minthao_2011 Jun 30 '16 at 09:01
  • @minthao_2011 The listings package would have to support utf8. On the other hand, minted does, but you need additional software for it, see http://www.ctan.org/pkg/minted. – Thomas F. Sturm Jun 30 '16 at 09:52
  • @minthao_2011: My bad, after skimming through the tcolorbox manual, I knew that I misunderstood your problem. I deleted my wrong answer. – Hoang-Ngan Nguyen Jul 01 '16 at 14:07
  • By the way, I followed Thomas' instruction and was able to compile your code. However, as pointed out by Thomas the font in listing does not look as good as the one in bài tập. To be able to compile this code, beside installing a bunch of additional LaTeX packages, I needed to install pygmentize on my Linux box ( for Windows you may try this answer http://tex.stackexchange.com/questions/108661/how-to-use-minted-under-miktex-and-windows-7 ). After that I ran pdflatex --shell-escape myfilename.tex. – Hoang-Ngan Nguyen Jul 01 '16 at 14:07
  • @ThomasF.Sturm: A similar trick might be hard. For example, how can I create these characters , ế, , ? These are missing character when using listingsutf8. – Hoang-Ngan Nguyen Jul 01 '16 at 14:15
  • @minthao_2011: Since VnTeX uses vnr font by default, if you want your listing to use the same font, you can add fontfamily=vnr to minted options. This works for me. – Hoang-Ngan Nguyen Jul 01 '16 at 16:52
  • @Hoang-NganNguyen The 'similar trick' is meant for adding 'literate' characters like the umlauts in my example. See http://tex.stackexchange.com/questions/110020/utf8-for-listings for more explanation. But I cannot say if this would be possible for Vietnamese characters too. – Thomas F. Sturm Jul 04 '16 at 05:24
  • @ThomasF.Sturm: Thank you, I will look at that. – Hoang-Ngan Nguyen Jul 04 '16 at 15:47