2

Please consider this MWE with stringstyle=\bfseries\color{blue}:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{color}
\usepackage{enumitem}

\usepackage{listings}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstset {%
    backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
        stringstyle=\bfseries\color{blue},      % <-- When using it the style does not apply because of ""
    basicstyle=\scriptsize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2,
    keepspaces=true,
    language=C++,
    columns=flexible,
}

\begin{document}

\begin{lstlisting}
"Example"
\end{lstlisting}

\end{document}

It is well known that the double quotation marks does not fit properly:

Quotation marks error

So I researched and I came across the two fantastic solutions that Ulrike Fischer and egreg propose in the same question!

Unfortunately, if I add any of the two solutions, two things happen:

  1. The style of quotation marks and the text inside it does not shown: Without style
  2. When I tried to copy that output it gaves me, for example using egreg second answer: “Example” (not "Example"). This is bad because in a compiler the first two characters “” make the code not compile correctly: Bad quotation marks

In fact, if it is also possible that in the PDF output the double quotes appear in the same way as after copying them as I want.

Is there an easy way to solve these two problems?

Thank you!!

P.S: Thank you to cfr I forgot to mention that I use LaTeX->PS->PDF and, if I add \usepackage[T1]{fontenc} then all the text inside it dissappear. Try adding that and test with more lines of code:

\begin{lstlisting}
"Example"

Code around here


and here
\end{lstlisting}

the output is:

Error

and here is the Build Output:

Build Output

  • I really think this is confusing to readers. If they need to type straight quotes in their code, the sample code should use straight quotes. If I see curly quotes in a code listing, I assume that I need curly quotes in my code. – cfr Jul 21 '18 at 22:21
  • Or are you asking how to get straight quotes? – cfr Jul 21 '18 at 22:22
  • @cfr for that I added the italic text: In fact, if it is also possible that in the PDF output the double quotes appear in the same way as after copying them as I want. I mean: if in the PDF we show that "Example" then in the compiler we need to use " " (using the same style of quotes). But I don't know how to modify it :(. – manooooh Jul 21 '18 at 22:23
  • Btw remember that there are another problem (the style is not shown in the PDF output). – manooooh Jul 21 '18 at 22:28
  • 1
    Anyway, try adding \usepackage{lmodern}. – cfr Jul 21 '18 at 22:59
2

You just need the T1 output encoding for fonts. (Note that this assumes one of the traditional TeX engines i.e. TeX or pdfTeX.)

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}
\usepackage{xcolor,textcomp}
\usepackage{listings}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstset {%
    backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
        stringstyle=\bfseries\color{blue},      % <-- When using it the style does not apply because of ""
    basicstyle=\scriptsize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2,
    keepspaces=true,
    language=C++,
    columns=flexible,
}

\begin{document}

\begin{lstlisting}
"Example"  "Example"
\end{lstlisting}
\end{document}

The second "Example" is copied back from the PDF rendered in Okular.

straight up

Note that this depends on the fonts you use in the document being available in the T1 encoding. For most people,

\usepackage[T1]{fontenc}

is all that's needed. This is true for anybody with a full version of TeX Live installed, for example.

If, however, you do not have Computer Modern available in the T1 format, you may get errors from pdfTeX or from the binary used to convert from DVI. In this case, either install the missing fonts (e.g. cm-super is one package providing CM in T1 format) or switch font packages (e.g. load lmodern or, better yet, obviously, cfr-lm).

  • 1
    @manooooh Can you please copy my code into a new .tex file and compile it using pdflatex? Do you then get the output I show? – cfr Jul 21 '18 at 22:50
  • 1
    @manooooh lmodern is not necessary. Only the T1 encoding is. But you do not have Computer Modern installed in the T1 encoding, so, although your DVI is fine, your PDF is not. However, most people do have CM installed in the T1 encoding, so lmodern is not necessary. (I like lmodern, but that's a different matter.) Note that if you have on-the-fly installation enabled, MikTeX should install it automatically - at least if you use pdfTeX rather than TeX. If you stick to TeX, you can always install cm-super in the package manager. – cfr Jul 21 '18 at 23:11
  • 1
    I've added a note. TeX is the engine. LaTeX is the format. I mean: you are using TeX as opposed to pdfTeX. On the command line, you're using latex rather than pdflatex. – cfr Jul 21 '18 at 23:15
  • 1
    Oh, that's because you're not using T1, too. 'á' doesn't occur in the default encoding, so TeX creates it from two characters, which is what you see when you copy. But if you use T1, a real 'á' is used, which should copy fine. This is because, when TeX was written, fonts could have only 128 characters. T1 has 256, so it deals much better with most accented characters. (Although something like 'tŷ' will unfortunately still not copy correctly. :() – cfr Jul 21 '18 at 23:28
  • 1
    When I use XeTeX or titlesec, there are explosions everywhere. – cfr Jul 21 '18 at 23:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.