[Tex/LaTex] not able to write bold characters using textbf

boldfont-encodingstypewriter

I'm working on latex and R (swaeave) and I am using utf8 encoding but when I use \textbf{dataset}, the text is not bold. Below is part of my code. But I am not sure what is reason behind it.

\documentclass[11pt,table,a4paper]{article}
\usepackage[T1,T2A]{fontenc}
\usepackage{CJKutf8}
\usepackage[english,russian]{babel}
\begin{document}
%chunk = 1
<<echo=FALSE,results=tex>>=
    cat("dataset","\n")
    cat("\\textbf{dataset}","\n")   # it should be bold  
 @
 \end{document}linecolor=goldenpoppy

The error is due to \usepackage[english,russian]{babel}. Please help me out in fixing this pblm.

Best Answer

UPDATE: See the solution at point 4.

I don't really understand the question. Apparently you are trying to insert some R code inside your latex document, but didn't take into account that some characters used in the listing have special meaning for latex.

For example, you have %chunk=1, but since % is the comment character for TeX, that line will be ignored. Then you have some \n inside, but since \ is the escape character for TeX, it complains about \n not being defined. Oddly enough, you doubled the \ before textbf, so textbf is not a tex command anymore in that context (while \\ is the command for a newline). Also you have # to introduce a comment in R, but # is a special char used by tex to denote an argument.

All these errors are not related to \usepackage[english,russian]{babel}, as you suggest. They are instead the usual errors which happen when you insert code from some other language into tex, without "protecting" it in a verbatim environment. Also, you have an extraneous linecolor=goldenpoppy after \end{document}, which will be ignored (what was it for, anyway?)

While more clarifications about your actual problem arrive, I can try guessing and suggesting some solutions.

  1. Solve the errors and get a compilable document. Use verbatim to disable the special meaning of all those special chars.

    \documentclass[11pt,table,a4paper]{article}
    \usepackage[T1,T2A]{fontenc}
    \usepackage{CJKutf8}
    \usepackage[english,russian]{babel}
    \begin{document}
    \begin{verbatim}
    %chunk = 1
    <<echo=FALSE,results=tex>>=
        cat("dataset","\n")
        cat("\\textbf{dataset}","\n")   # it should be bold
     @
    \end{verbatim}
    \end{document}
    

    Result:

    verbatim

  2. Use listings package to get syntax highlight for R (comments are italized, keywords are bold, etc.)

    \documentclass[11pt,table,a4paper]{article}
    \usepackage[T1,T2A]{fontenc}
    \usepackage{CJKutf8}
    \usepackage[english,russian]{babel}
    \usepackage{listings}
    \begin{document}
    \begin{lstlisting}[language=R]
    %chunk = 1
    <<echo=FALSE,results=tex>>=
    cat("dataset","\n")
    cat("\\textbf{dataset}","\n")   # it should be bold
    @
    \end{lstlisting}
    \end{document}
    

    Result:

    listings

  3. I guess that you want the text "dataset" to be typeset in bold. If you use verbatim, the command \textbf is not "executed" (because \ has lost its status of escape char). So you need a verbatim environment which disables most of the scpecial chars, but still allows for \ as escape. Fancyvrb package has such an option. Unfortunately in that case \n would be interpreted also as a command. An easy solution is to define that command.

    \documentclass[11pt,table,a4paper]{article}
    \usepackage[T1,T2A]{fontenc}
    \usepackage{CJKutf8}
    \usepackage[english,russian]{babel}
    \usepackage{fancyvrb}
    \def\n{\textbackslash n}
    \begin{document}
    \begin{Verbatim}[commandchars=\\\{\}]
    %chunk = 1
    <<echo=FALSE,results=tex>>=
    cat("dataset","\n")
    cat("\textbf{dataset}","\n")   # it should be bold
    @
    \end{Verbatim}
    \end{document}
    

    But, oh! The result shows no bold (perhaps this was your actual problem?):

    fancyvrb

    In this case this is because your tt font has not bold variant. And this could be related to the cyrillic fonts you use. Please, confirm if this is the intended question.

  4. UPDATED: Once it has been confirmed (in a comment) that the problem was not the syntax errors found in the (non-working) minimal example, but instead the lack of an appropiate tt bold font, the solution is to provide such a font. Typically the solution would be as simple as to \usepackage{extra-bold}, which installs bold versions of cmtt font. However this does not work either in this case, because this package installs those fonts under the 'OT1' enconding, but the document uses T2A encoding.

    A "brute force" solution is to fool latex into "thinking" that font cmttb has T2A encoding. This works for the minimal example provided because the listing contains only ascii characters. I don't know what would happen if the listing had contained cyrillic or chinese characters. You have to try yourself.

    In order to implement this "solution" (note the quotes), you have to write a file, called myboldtt.sty for example, in the same folder than your main document. The content of this file would be:

    \providecommand{\EC@ttfamily}[5]{%
      \DeclareFontShape{#1}{#2}{#3}{#4}%
      {<5><6><7><8>#50800%
       <9><10><10.95><12><14.4><17.28><20.74><24.88><29.86>%
       <35.83>genb*#5}{}}
    \DeclareFontFamily{T2A}{cmtt}{\hyphenchar\font\m@ne}
    \EC@ttfamily{T2A}{cmtt}{m}{n}{latt}
    \EC@ttfamily{T2A}{cmtt}{m}{sl}{last}
    \EC@ttfamily{T2A}{cmtt}{m}{it}{lait}
    \EC@ttfamily{T2A}{cmtt}{m}{sc}{latc}
    \EC@ttfamily{T2A}{cmtt}{bx}{n}{labtl}
    \DeclareFontShape{T2A}{cmtt}{b}{n}
       {
        <5><6><7><8><9><10><12><10.95><14.4><17.28><20.74><24.88>cmttb10
        }{}
    \DeclareFontShape{T2A}{cmtt}{bx}{n}%
       {<->ssub*cmtt/b/n}{}
    \endinput
    

    And then ensure that \usepackage{myboldtt} appears in your main document. A MWE follows:

    \documentclass[11pt,table,a4paper]{article}
    \usepackage{bold-extra}
    \usepackage[T1,T2A]{fontenc}
    \usepackage{CJKutf8}
    \usepackage[english,russian]{babel}
    \usepackage{fancyvrb}
    \usepackage{myboldtt}
    \def\n{\textbackslash n}
    \begin{document}
    Regular text. \textbf{Bold}.
    \begin{Verbatim}[commandchars=\\\{\}]
    %chunk = 1
    <<echo=FALSE,results=tex>>=
    cat("dataset","\n")
    cat("\textbf{dataset}","\n")   # it should be bold
    @
    \end{Verbatim}
    \end{document}
    

    Which (at last!) produces the intended output:

    Bold tt

    NOTE: You need to have cm-mf-extra fonts also installed in your system.

Related Question