偽代碼中的引號目前如下所示:
如何獲得看起來更像程式設計環境而不是像我只是用 MS Word 輸入的報價?
微量元素
\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc} % this is needed for correct output of umlauts in pdf
\usepackage{amssymb,amsmath,amsfonts} % nice math rendering
\usepackage{algorithm,algpseudocode}
\begin{document}
\begin{preview}
This is a 'samle' with some "quotes".
\begin{algorithm}[H]
\begin{algorithmic}
\State $a \gets \Call{map}{~}$
\State $a['x'] \gets 42$
\State $a["x"] \gets 1337$
\end{algorithmic}
\caption{Algorithmus von Stoer und Wanger}
\label{alg:seq1}
\end{algorithm}
\end{preview}
\end{document}
相關問題
這個問題已經在其他一些環境中得到了答案:
- 直接引用?:似乎我們可以使用
\textquotedbl
and得到直引號\textquotesingle
,但我希望所有演算法環境都可以直接引號,而不必在我的偽代碼中擺弄 - Consolas:直引號:這是用於列表的
- 如何在清單中獲得直接雙引號?
- Tab 中的直引號
答案1
只要你使用一些技巧,你就可以得到直接的引號。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{textcomp}
\usepackage{amssymb,amsmath,amsfonts}
\usepackage{algorithm,algpseudocode}
\usepackage{etoolbox} % for the trick
% setup algorithmic to use straight quotes
\AtBeginEnvironment{algorithmic}{\useupquotes}
% define the command that activates the quotes and redefines them
\newcommand{\useupquotes}{%
\begingroup\lccode`\~=`\'\lowercase{\endgroup\let~}\algoupquote
\begingroup\lccode`\~=`\"\lowercase{\endgroup\let~}\algoupquotes
\catcode`\'=\active\catcode`\"=\active
}
% Customize here (\mbox is necessary because of math mode;
% if needed in subscripts, use \text instead)
\newcommand{\algoupquote}{\mbox{\textquotesingle}}
\newcommand{\algoupquotes}{\mbox{\char`\"}}
\begin{document}
Look at Algorithm~\ref{some} for seeing the quotes.
\begin{algorithm}[htp]
\begin{algorithmic}
\State $a['x'] \gets 42$
\State $a["x"] \gets 1337$
\end{algorithmic}
\caption{Some algorithm}\label{some}
\end{algorithm}
\end{document}
答案2
這是主動角色方法,也使用textcomp
.
\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc} % this is needed for correct output of umlauts in pdf
\usepackage{amssymb,amsmath,amsfonts} % nice math rendering
\usepackage{algorithm,algpseudocode}
\usepackage{textcomp}
\let\svalgorithm\algorithm
\catcode`'=\active
\catcode`"=\active
\def\quoteactive{\catcode`'=\active\def'{\makebox{\textquotesingle}}}
\def\qquoteactive{\catcode`"=\active\def"{\makebox{\textquotedbl}}}
\catcode`'=12
\catcode`"=12
\def\algorithm{\quoteactive\qquoteactive\svalgorithm}
\begin{document}
\begin{preview}
This is a 'sample' with some "quotes"{} or ``quotes''.
\begin{algorithm}[H]
\begin{algorithmic}
\State $a \gets \Call{map}{~}$
\State $a['x'] \gets 42$
\State $a["x"] \gets 1337$
\end{algorithmic}
\caption{Algorithmus von Stoer und Wanger}
\label{alg:seq1}
\end{algorithm}
Restored? ' and ", ``quotations'' or `quotations'
\end{preview}
\end{document}
答案3
您可能可以使引號處於活動狀態,但我建議將它們包裝在巨集中(參考將在直接引用?):
\documentclass{article}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc} % this is needed for correct output of umlauts in pdf
\usepackage{textcomp}
\usepackage{amssymb,amsmath,amsfonts} % nice math rendering
\usepackage{algorithm,algpseudocode}
\makeatletter
\newcommand{\upquotetype}{}
\newcommand{\upquote@aux}[1]{\text{\upquotetype}#1\text{\upquotetype}}
\newcommand{\upquotesingle}{\renewcommand{\upquotetype}{\textquotesingle}\upquote@aux}
\newcommand{\upquotedouble}{\renewcommand{\upquotetype}{\textquotedbl}\upquote@aux}
\begin{document}
This is a 'sample' with some "quotes".
\begin{algorithm}[H]
\begin{algorithmic}
\State $a[\upquotesingle{x}] \gets 42$
\State $a[\upquotedouble{x}] \gets 1337$
\end{algorithmic}
\caption{Some algorithm}
\end{algorithm}
\end{document}