引號的自動撇號

引號的自動撇號

這方面還有一些其他問題解決相關問題,但我還沒有找到任何完全涵蓋我的問題的問題。

我有一個文字(從 XML 轉換而來),它使用具有三種不同含義的撇號':開始引用、結束引用以及縮寫和屬格(can'tcats'等)。有沒有辦法保留撇號'原來的樣子除開頭引號外的所有情況,其中必須替換為 `?供您參考,我實際上在原始文件中使用了 XeLaTeX,但我不確定這是否重要。

\documentclass{memoir}

\begin{document}

He said, 'the cats' hats can't fit me'.

\end{document}

我想確定使用哪一種的方法是:

  • 如果撇號前面有文字字符,則保留它
  • 如果它前面有任何其他內容(包括空格並且什麼都沒有),請將其更改為 `

筆記:單字開頭的縮寫撇號(例如 in 'tis)不太可能出現在我的文件中。

答案1

我認為只有當前面有空格或什麼都沒有時,您才需要(開放式引用) 。'

這也適用於數學模式。

\documentclass{memoir}

\makeatletter
\let\apostrophe'
\catcode`'=\active
\protected\def'{%
  \ifvmode
    `%
  \else
    \ifmmode
      \expandafter\expandafter\expandafter\active@math@prime % for math
    \else
      \ifdim\lastskip>0pt
        `%
      \else
        \apostrophe
      \fi
    \fi
  \fi
}
% redefine \pr@m@s to look for an active '
\def\pr@m@s{%
  \ifx'\@let@token
    \expandafter\pr@@@s
  \else
    \ifx^\@let@token
      \expandafter\expandafter\expandafter\pr@@@t
    \else
      \egroup
    \fi
  \fi}
\makeatother

\begin{document}

He said, 'the cats' hats can't fit me'.

'Ouch,' he said.

Some 'derivatives' $f'+f''+f'''$.

\end{document}

在此輸入影像描述

請注意,根據您的規範,第二行在逗號後將有一個開放引號。

筆記

  • \protected已使用或\section{A 'quoted'}將失敗
  • \section{'quoted'}將會失敗並且對此無能為力

以下內容似乎解決了出現尾隨引號時節和章節標題的問題。

兩者一起編譯pdflatexxelatex結果是一樣的。

\documentclass{memoir}

\usepackage{ifxetex}
\ifxetex
  \usepackage{fontspec}
%  \setmainfont{Linux Libertine O}
\fi

\makeatletter
% fix latex's habit of resetting ' during \write
\begingroup
\obeylines\obeyspaces%
\catcode`\'\active%
\gdef\@resetactivechars{%
\def^^M{\@activechar@info{EOL}\space}%
\def {\@activechar@info{space}\space}}%
\endgroup

\renewcommand{\cftchapteraftersnumb}{\hskip1sp\relax}
\let\ORIM@sect\M@sect
\def\M@sect#1#2#3#4#5#6[#7][#8]#9{%
  \ORIM@sect{#1}{#2}{#3}{#4}{#5}{#6}[\hskip1sp\relax#7][\hskip1sp\relax#8]{\hskip1sp\relax#9}%
}
\catcode`'=\active
\protected\def'{%
  \ifvmode
    `%
  \else
    \ifmmode
      \expandafter\expandafter\expandafter\active@math@prime % for math
    \else
      \relax
      \ifdim\lastskip=1sp
        `%
      \else
        \ifdim\lastskip>0pt
          `%
        \else
          \rq
        \fi
      \fi
    \fi
  \fi
}
% redefine \pr@m@s to look for an active '
\def\pr@m@s{%
  \ifx'\@let@token
    \expandafter\pr@@@s
  \else
    \ifx^\@let@token
      \expandafter\expandafter\expandafter\pr@@@t
    \else
      \egroup
    \fi
  \fi}
\makeatother

\begin{document}
\tableofcontents
\chapter{'Chapter'}

\section{'Quoted'}

He said, 'the cats' hats can't fit me'.

'Ouch,' he said.

Some 'derivatives' $f'+f''+f'''$.

\end{document}

答案2

如果不需要區分撇號和結束引號,那麼你可以嘗試:

\def\leftquote{`}\def\rightquote{'}

\catcode`\'=13
\def'{\ifvmode\leftquote \else \ifdim\lastskip=0pt \rightquote \else \leftquote\fi\fi}

He said, 'the cats' hats can't fit me'.

如果文件使用這樣的「雙引號」並且您需要對其進行排版,那麼您可以使用以下定義:

\def\leftqquote{``}\def\rightqqoute{''} % or another definition, can depend on fonts
                                        % put this definition before \catcode`\'=13
\catcode`\"=13
\def"{\bgroup\def"{\rightqqoute\egroup}\leftqquote}

The normal "quotes" are here.

編輯:因為有關於'數學模式的評論,例如“$f”$,我在這裡添加此功能:

\def\leftquote{`}\def\rightquote{'}

\catcode`\'=13
\def'{\ifmmode \expandafter\mathprime \else\ifvmode \leftquote \else
        \ifdim\lastskip=0pt \rightquote \else \leftquote\fi\fi\fi}
\def\mathprime{^\bgroup\catcode`\'=12 \csname prim@s\endcsname}

與接受的答案不同,我不重寫\pr@m@s宏,只是將 catcode 暫時設定回 12 \prim@s

答案3

如果我正確理解您的工作流程,看起來(a)有一個流程創建了一個存在一些問題的可編譯 .tex 文件,(b)您需要在通過 LaTeX 運行 .tex 文件之前解決這些問題。此外,主要問題似乎是整個 tex 檔案中都使用了“啞”(單)引號,並且您希望找到一種方法用反引號字元(也稱為撇號字元)替換一些單引號字元(又稱反引號)。您沒有明確指出您的文件使用哪種語言,因此我現在假設它是英語。

  • 如果有問題的撇號字元僅出現在句子中間位置(因此前面有空格字元)而不是出現在行首,我想您可以啟動編輯器並將所有實例替換space-apostrophespace-backtick

  • 如果有問題的撇號字元可能出現在行首,您需要檢查您的編輯器是否可以執行newline_char-apostrophefor的搜尋和取代newline_char-backtick。由於您沒有指明您選擇哪個編輯器(如果有),因此我無法提供有關如何完成此任務的更具體的建議。

警告:這兩種方法(以及類似的自動化方法)都會不是如果您有“文學”表達,例如'tis「it is」的縮寫),則會產生正確的結果。 (在這種情況下,撇號表示省略而不是引用的開始,因此撇號應該不是替換為反引號。

相關內容