外部テキストのアンパサンドを非表示にする

外部テキストのアンパサンドを非表示にする

私は、データツール パッケージを使用して外部ファイルからデータをインポートし、いくつかの文字を作成します。会社名には特殊文字 (アンパサンド "&") が含まれている可能性があるため、会社名を正しく取得する以外はすべてスムーズに動作します。したがって、会社名 (例: "Father & Sons") をインポートし、後でドキュメントで使用するために文字列変数に格納します。

私の質問は、「&」やその他の特殊文字の解釈を抑制する方法があるかどうかです。 and を使用してみましたverbatimが、alltt良い結果は得られませんでした。 あるいは、外部ファイル内の s を置き換えることもできます&が、インポートしたテキストをそのまま書き込むだけの汎用的なソリューションが欲しいです。

以下の例は簡略化されていますが、問題を示しています。

\documentclass[10pt]{article}
\usepackage[a4paper]{geometry}

\newcommand{\txtstring}{Father & Sons}
\newcommand{\txtstringtwo}{Father \& Sons}

\begin{document}

\txtstring
\par
\txtstringtwo

\end{document}

表示されるエラーは「配置が誤っているタブ文字 &. \txtstring」で、これは「&」が原因です。したがって、「&」を解釈せずに文字列変数を解決できますか?


Henri Menke (および Ulrike Fischer) が提案した「\detokenize」を使用するソリューションで、この問題を解決できます。egreg の提案に従って、datatool パッケージを使用した「実際の」例を追加しました。

\documentclass[10pt]{article}

%%% Definer papiropsætning og margin %%%%%%%%%%%%%%%%%%%%%%%
\usepackage[a4paper]{geometry}
\usepackage{datatool}     % access .csv
\usepackage[absolute]{textpos}
\usepackage{lipsum}


%%% Definer ingen paragraf indent  %%%%%%%%%%%%%%%%%%%%%%%%%
\setlength{\parindent}{0pt}

\newcommand{\txtstring}{Father & Sons}
\newcommand{\txtstringtwo}{Father \& Sons}


%%% Define database load (CSV) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\DTLsetseparator{;}       % use semi-colon as delimter (commas use in quotations)
%\DTLloaddb[noheader,keys={CVR,CompanyName,LastName,FirstName,Address,PostCode,City}]{data}{test2.csv}
%\DTLloaddb[noheader,keys={CVR,CPR,DatoStart,DatoSlut}]{data_detail}{test_detail.csv}


%%% Here I create a database %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\DTLnewdb{testdata}
\DTLnewrow{testdata}%
\DTLnewdbentry{testdata}{CVR}{12 23 45 56}%
\DTLnewdbentry{testdata}{CompanyName}{Fox & Sons}%
\DTLnewdbentry{testdata}{LastName}{Fox}%
\DTLnewdbentry{testdata}{FirstName}{Red}%
\DTLnewdbentry{testdata}{Address}{Fowl Street 1}%
\DTLnewdbentry{testdata}{PostCode}{4567}%
\DTLnewdbentry{testdata}{City}{Fox Town}%
\DTLnewrow{testdata}%
\DTLnewdbentry{testdata}{CVR}{45 56 67 78}%
\DTLnewdbentry{testdata}{CompanyName}{Elephant & Daughters}%
\DTLnewdbentry{testdata}{LastName}{Fant}%
\DTLnewdbentry{testdata}{FirstName}{Eli}%
\DTLnewdbentry{testdata}{Address}{Africa Road 1}%
\DTLnewdbentry{testdata}{PostCode}{1259}%
\DTLnewdbentry{testdata}{City}{Mega City}%


\begin{document}

%%% Here I read from databse an create one letter per line %
\DTLforeach{testdata}{%
    \CV=CVR,%
    \CN=CompanyName,%
    \LN=LastName,%
    \FN=FirstName,%
    \AD=Address,%
    \PC=PostCode,%
    \CT=City%
}{%


%%% Write sender %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{textblock*}{\textwidth}[0,0](25mm,37mm)
    \scriptsize%
    \textbf{Afs: SomeStreet 1, 1234 City}
\end{textblock*}


%%% Write recipient %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{textblock*}{\textwidth}[0,0](25mm,42mm)
    \detokenize\expandafter{\CN}\\
    \detokenize\expandafter{\FN}~\detokenize\expandafter{\LN}\\%
    \detokenize\expandafter{\AD}\\%
    \detokenize\expandafter{\PC}~\detokenize\expandafter{\CT}%
\end{textblock*}


%%% Write heading %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{textblock*}{\textwidth}[0,0](20mm,98mm)
\textbf{\Large Here is my header}\\

\lipsum[4]

\end{textblock*}

\null\clearpage

}

\end{document}

答え1

\DTLloadrawdbの代わりにを使用するソリューションを次に示します\DTLloaddb

\documentclass{article}

\usepackage{filecontents}
\usepackage{datatool}

\begin{filecontents*}{test.csv}
CVR,CompanyName,LastName,FirstName,Address,PostCode,City
12 23 45 56,Fox & Sons,Fox,Red,Fowl Street 1,4567,Fox Town
45 56 67 78,Elephant & Daughters,Fant,Eli,Africa Road 1,1259,Mega City
\end{filecontents*}

\DTLloadrawdb{testdata}{test.csv}
\begin{document}
\DTLforeach{testdata}%
{%
    \CV=CVR,%
    \CN=CompanyName,%
    \LN=LastName,%
    \FN=FirstName,%
    \AD=Address,%
    \PC=PostCode,%
    \CT=City%
}%
{%
  \CV, \CN, \LN, \FN, \AD, \PC, \CT\par
}
\end{document}

アンパサンドは、\&CSV ファイルからデータが読み取られるときに自動的に に変換されます。\DTLforeachループ内で特別な操作を行う必要はありません。上記の例では、次の結果が生成されます。

文書の画像

他の 8 つの特殊文字についても同様です。マッピングされない唯一の特殊文字はバックスラッシュです。独自のマッピングを追加することもできます ( の前\DTLloadrawdb)。例:

\DTLrawmap{£}{\pounds}

答え2

データの読み取り時に & の catcode をローカルに設定できます。これは、後で & の使用を妨げることはなく、\detokenize とは異なり、入力内の非 ASCII 入力などの他のコマンドを壊すこともありません。

    \documentclass[10pt]{article}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage[a4paper]{geometry}

    \catcode`\&=12
    \newcommand{\txtstring}{Father & Sons & Mütter}
    \newcommand{\txtstringtwo}{Father \& Sons & Mütter}
    \newcommand{\txtstringthree}{\detokenize{Father & Sons & Mütter}}
    \catcode`\&=4

    \begin{document}

    \txtstring: OK
    \par
    \txtstringtwo: OK
    \par
    \txtstringthree: Bad

    \begin{tabular}{ll}
    blub & bla
    \end{tabular}
    \end{document}

ここに画像の説明を入力してください

関連情報