5

I need to display source code using the tabbing environment and \sffamily. (Please note that I cannot use neither listings nor verbatim since I cannot dynamically set my tab stops).

I defined an environment sftabbing for doing this. A Minimal Working Example is as follows

\documentclass{article}

\newenvironment{sftabbing}
  {\par\sffamily\small\tabbing}
  {\endtabbing\par}

\begin{document}

\begin{sftabbing}
INSERT \= INTO Shippers(CompanyName, Phone) \\
\> VALUES ('Federal Express', '02 752 75 75') \\ \\
UPDATE \= Shippers \\
SET \> CompanyName='Fedex' \\
WHERE \> CompanyName='Federal Express' \\ \\
SELECT \> ProductID, ProductName \\
FROM \> Products P \\
WHERE \> EXISTS ( \\
\> SELECT \= * \\
\> FROM \> Orders O JOIN Customers C ON \\
\> \> O.CustomerID = C.CustomerID JOIN \\
\> \> OrderDetails D ON O.OrderID = D.OrderID \\
\> WHERE \> C.Country = 'Germany' AND D.ProductID = P.ProductID )
\end{sftabbing}

\end{document}

However, in this case the quotes are transformed into curly quotes and I would need they remain straight quotes. How to achieve this ?

I know that the package upquote achieve this for the verbatim environment.

By the way, is there any way to improve the definition of my environment so that I do not need to write \\ to change lines ?

0

3 Answers 3

3

You can use textcomp that has a straight quote glyph and locally activate the quote character so that it produces that glyph.

\documentclass{article}

\usepackage{textcomp}

\newenvironment{sftabbing}
 {\par
  % locally define the active quote
  \begingroup\lccode`~=`' \lowercase{\endgroup\let~}\textquotesingle
  % locally activate the quote
  \catcode`'=\active
  \sffamily\small\tabbing}
 {\endtabbing\par}

\begin{document}

\begin{sftabbing}
INSERT \= INTO Shippers(CompanyName, Phone) \\
\> VALUES ('Federal Express', '02 752 75 75') \\ \\
UPDATE \= Shippers \\
SET \> CompanyName='Fedex' \\
WHERE \> CompanyName='Federal Express' \\ \\
SELECT \> ProductID, ProductName \\
FROM \> Products P \\
WHERE \> EXISTS ( \\
\> SELECT \= * \\
\> FROM \> Orders O JOIN Customers C ON \\
\> \> O.CustomerID = C.CustomerID JOIN \\
\> \> OrderDetails D ON O.OrderID = D.OrderID \\
\> WHERE \> C.Country = 'Germany' AND D.ProductID = P.ProductID )
\end{sftabbing}

\end{document}

enter image description here

For double quotes the best is to do

\usepackage[T1]{fontenc}

so that " will do by itself. However, if you have reasons not to do it, here's a workaround:

\documentclass{article}

\usepackage[T1,OT1]{fontenc} % keep OT1 as default
\usepackage{textcomp}

\newenvironment{sftabbing}
 {\par
  % locally define the active quote
  \begingroup\lccode`~=`' \lowercase{\endgroup\let~}\textquotesingle
  \begingroup\lccode`~=`" \lowercase{\endgroup\def~}{{\fontencoding{T1}\selectfont\textquotedbl}}
  % locally activate the quote
  \catcode`'=\active \catcode`"=\active
  \sffamily\small\tabbing}
 {\endtabbing\par}

\begin{document}

Quotes: "

\begin{sftabbing}
INSERT \= INTO Shippers(CompanyName, Phone) \\
\> VALUES ('Federal Express', "02 752 75 75") \\ \\
UPDATE \= Shippers \\
SET \> CompanyName='Fedex' \\
WHERE \> CompanyName='Federal Express' \\ \\
SELECT \> ProductID, ProductName \\
FROM \> Products P \\
WHERE \> EXISTS ( \\
\> SELECT \= * \\
\> FROM \> Orders O JOIN Customers C ON \\
\> \> O.CustomerID = C.CustomerID JOIN \\
\> \> OrderDetails D ON O.OrderID = D.OrderID \\
\> WHERE \> C.Country = 'Germany' AND D.ProductID = P.ProductID )
\end{sftabbing}

\end{document}

enter image description here

Note, however, that using " for getting curly closing quotes is disputable to begin with. See Why this " (one symbol) works as one double quote as well

4
  • Many thanks! This is indeed what I needed. But I would need the same behaviour for the double quotes. How to extend your definition ? I know the T1 font enconding provides the \textquotedbl character. Commented Apr 22, 2014 at 15:26
  • @EstebanZimanyi If you have nothing that prevents \usepackage[T1]{fontenc}, this would be sufficient: using " would produce straight quotes.
    – egreg
    Commented Apr 22, 2014 at 15:33
  • Alas, I would need this behaviour only within the tabbing environment, otherwise I would need to change all the 650+ pages of my manuscript looking for " (one double quote) and replacing them by '' (two single quotes) Commented Apr 22, 2014 at 15:44
  • @EstebanZimanyi Using " for producing closing double quotes is wrong to begin with. See Why this one symbol " works as one double quote as well
    – egreg
    Commented Apr 22, 2014 at 16:02
2

You can use the upquote package with \textquotesingle. Since it's rather cumbersome to type \textquotesingle I usually define a shorter command, such as the \upsq one in the example below:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{textcomp}
\usepackage{upquote}

\newcommand*{\upsq}[1]{\textquotesingle #1\textquotesingle}

\newenvironment{sftabbing}
  {\par\sffamily\small\tabbing}
  {\endtabbing\par}

\begin{document}

\begin{sftabbing}
INSERT \= INTO Shippers(CompanyName, Phone) \\
\> VALUES (\upsq{Federal Express}, \upsq{02 752 75 75}) \\ \\
UPDATE \= Shippers \\
SET \> CompanyName=\upsq{Fedex} \\
WHERE \> CompanyName=\upsq{Federal Express} \\ \\
SELECT \> ProductID, ProductName \\
FROM \> Products P \\
WHERE \> EXISTS ( \\
\> SELECT \= * \\
\> FROM \> Orders O JOIN Customers C ON \\
\> \> O.CustomerID = C.CustomerID JOIN \\
\> \> OrderDetails D ON O.OrderID = D.OrderID \\
\> WHERE \> C.Country = \upsq{Germany} AND D.ProductID = P.ProductID )
\end{sftabbing}

\end{document}

This produces:

Image of result

1

Reading the docs of upquote package indicates that, for the standard CM fonts, a \ttfamily\char13 already produces the desired glyph, so I just made the ' active and redefined it.

EDITED so that straight quote is only active in the sftabbing environment, and restores to normal quote thereafter.

EDITED to include cmap package which allows proper cut/paste of quote character from PDF (see Can't copy-paste from my PDF. Any idea why?)

ADDENDUM added for those for whom cmap approach does not work.

\RequirePackage{cmap}
\documentclass{article}

\newenvironment{sftabbing}
  {\par\sffamily\small\squoteon\tabbing}
  {\endtabbing\squoteoff\par}

\let\svquote'
\catcode`'=\active
\def\squoteon{\catcode`'=\active%
  \def'{\setbox0=\hbox{\ttfamily\char13}\raisebox{1pt}{\box0}}}
\def\squoteoff{\catcode`'=12\let\'\svquote}
\catcode`'=12

\begin{document}

\begin{sftabbing}
INSERT \= INTO Shippers(CompanyName, Phone) \\
\> VALUES ('Federal Express', '02 752 75 75') \\ \\
UPDATE \= Shippers \\
SET \> CompanyName='Fedex' \\
WHERE \> CompanyName='Federal Express' \\ \\
SELECT \> ProductID, ProductName \\
FROM \> Products P \\
WHERE \> EXISTS ( \\
\> SELECT \= * \\
\> FROM \> Orders O JOIN Customers C ON \\
\> \> O.CustomerID = C.CustomerID JOIN \\
\> \> OrderDetails D ON O.OrderID = D.OrderID \\
\> WHERE \> C.Country = 'Germany' AND D.ProductID = P.ProductID )
\end{sftabbing}
Test's of quote
\end{document}

Zoom:

enter image description here

For comparison, the straight quote activated by the textcomp package in \sffamily looks like a rectangular block, which may be more in keeping with the sf style, but which I find unattractive:

enter image description here

I thus chose to retain the \ttfamily look for the quote character.


ADDENDUM:

For those for whom the cmap package does not provide relief for the copy/paste feature from PDFs, an alternative "glyph-to-unicode" approach is here. Both methods work for me, but the OP was having difficulty with the cmap approach.

\documentclass{article}
\newenvironment{sftabbing}
  {\par\sffamily\small\squoteon\tabbing}
  {\endtabbing\squoteoff\par}

\let\svquote'
\catcode`'=\active
\def\squoteon{\catcode`'=\active%
  \def'{\setbox0=\hbox{\ttfamily\char13}\raisebox{1pt}{\box0}}}
\def\squoteoff{\catcode`'=12\let\'\svquote}
\catcode`'=12

\pdfgentounicode=1 %
\input glyphtounicode.tex %

\begin{document}

\begin{sftabbing}
INSERT \= INTO Shippers(CompanyName, Phone) \\
\> VALUES ('Federal Express', '02 752 75 75') \\ \\
UPDATE \= Shippers \\
SET \> CompanyName='Fedex' \\
WHERE \> CompanyName='Federal Express' \\ \\
SELECT \> ProductID, ProductName \\
FROM \> Products P \\
WHERE \> EXISTS ( \\
\> SELECT \= * \\
\> FROM \> Orders O JOIN Customers C ON \\
\> \> O.CustomerID = C.CustomerID JOIN \\
\> \> OrderDetails D ON O.OrderID = D.OrderID \\
\> WHERE \> C.Country = 'Germany' AND D.ProductID = P.ProductID )
\end{sftabbing}
Test's of quote
\end{document}
6
  • 1
    Based on your idea, I improved your solution to be able to have the same behaviour for double quotes. Commented Apr 22, 2014 at 16:24
  • A minor problem is that when I copy/paste from the resulting pdf I obtain additional spaces as in SET CompanyName= ' Fedex ' instead of SET CompanyName='Fedex' and the former is incorrect in SQL. This will prevent readers to copy/paste the code from the manuscript and test it directly on the computer. Commented Apr 22, 2014 at 16:38
  • @EstebanZimanyi Please see revision. Package cmap was required. Commented Apr 22, 2014 at 17:28
  • The package cmap does not solve the problem :-( A possible diagnostics is that \begin{tabbing} \raisebox{1pt}{\ttfamily\char13}Blah blah\raisebox{1pt}{\ttfamily\char13} \end{tabbing} produces the output 'Blah blah' when I copy/paste from the pdf file, while \begin{sftabbing} \raisebox{1pt}{\ttfamily\char13}Blah blah\raisebox{1pt}{\ttfamily\char13} \end{sftabbing} produces the output ' Blah blah ' (with extra spaces) when I copy/paste from the pdf file. Commented Apr 22, 2014 at 21:43
  • @EstebanZimanyi Using Adobe Acrobat or Adobe Reader, my copy paste works fine on this code. Perhaps one should check if packages are up to date. Commented Apr 22, 2014 at 22:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .