
多数の表を tex ファイルに入力し、各表の「括弧内の標準エラー」を「任意のもの」に置き換えたいと考えています。
例
\documentclass[12pt,a4paper]{article}
\usepackage[UTF8]{ctex}
\begin{document}
\input{table.tex}
\input{table.tex}
\end{document}
テーブル.tex
\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{table \label{tab3}}
\begin{tabular}{l*{3}{c}}
\hline\hline
&\multicolumn{1}{c}{(1)E}&\multicolumn{1}{c}{(2)W}&\multicolumn{1}{c}{(3)M}\\
\hline
ln$\chi$ & 3.163\sym{***}& 4.133\sym{***}& 2.017\sym{***}\\
& (0.696) & (0.451) & (0.442) \\
\hline
FE & Y & Y & Y \\
N & 1005 & 980 & 825 \\
R2 & 0.660 & 0.583 & 0.624 \\
\hline\hline
\multicolumn{4}{l}{\footnotesize Standard errors in parentheses}\\
\multicolumn{4}{l}{\footnotesize \sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \(p<0.001\)}\\
\end{tabular}
\end{table}
IDE:Texpad 1.731
エンジン: Xelatex、BibTex
答え1
LuaLaTeX の使用が選択肢にある場合は、次の例に示すアプローチが役立つ可能性があります。 という名前の Lua 関数はdosub
、文字列置換操作を実行するように設定されており、この関数の操作を環境に制限するための LaTeX コードが提供されていますtable
。したがって、環境内以外で発生する可能性のある文字列「括弧内の標準エラー」のインスタンスはtable
そのまま残されます。
% !TeX program = lualatex
\RequirePackage{filecontents}
%% create the files 'table1.tex' and 'table2.tex'
\begin{filecontents}{table1.tex}
\begin{table}[htbp]
\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{A first table \label{tab1}}
\begin{tabular}{l*{3}{c}}
\hline\hline
&\multicolumn{1}{c}{(1)E}&\multicolumn{1}{c}{(2)W}&\multicolumn{1}{c}{(3)M}\\
\hline
$\ln\chi$ & 3.163\sym{***}& 4.133\sym{***}& 2.017\sym{***}\\
& (0.696) & (0.451) & (0.442) \\
\hline
FE & Y & Y & Y \\
N & 1005 & 980 & 825 \\
R\sym{2} & 0.660 & 0.583 & 0.624 \\
\hline\hline
\multicolumn{4}{l}{\footnotesize Standard errors in parentheses}\\
\multicolumn{4}{l}{\footnotesize \sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \(p<0.001\)}\\
\end{tabular}
\end{table}
\end{filecontents}
\begin{filecontents}{table2.tex}
\begin{table}[htbp]
\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{A second table \label{tab2}}
\begin{tabular}{l*{3}{c}}
\hline\hline
&\multicolumn{1}{c}{(1)A}&\multicolumn{1}{c}{(2)B}&\multicolumn{1}{c}{(3)C}\\
\hline
$\ln\chi$ & 3.163\sym{***}& 4.133\sym{***}& 2.017\sym{***}\\
& (0.696) & (0.451) & (0.442) \\
\hline
FE & Y & Y & Y \\
N & 1005 & 980 & 825 \\
R\sym{2} & 0.660 & 0.583 & 0.624 \\
\hline\hline
\multicolumn{4}{l}{\footnotesize Standard errors in parentheses}\\
\multicolumn{4}{l}{\footnotesize \sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \(p<0.001\)}\\
\end{tabular}
\end{table}
\end{filecontents}
\documentclass[12pt,a4paper]{article}
%%\usepackage[UTF8]{ctex}
%>>>> --- new code starts here ---
\usepackage{luacode,etoolbox}
\begin{luacode}
function dosub ( s )
return ( string.gsub ( s , "Standard errors in parentheses",
"Anything I want" ) )
end
\end{luacode}
\AtBeginEnvironment{table}{\directlua{
luatexbase.add_to_callback("process_input_buffer", dosub, "dosub" ) }}
\AtEndEnvironment{table}{\directlua{
luatexbase.remove_from_callback("process_input_buffer", "dosub" ) }}
%<<<< --- new code ends here ---
\begin{document}
\input table1
This instance of ``Standard errors in parentheses'' is not modified.
\input table2
\end{document}
答え2
Unix ベースのシステムを使用している場合は、 GNUgrep
と GNU を使用します。perl
任意の数のファイル内の任意のテキストを置き換える予定の場合は、コマンド ライン ターミナルの使い方を知っておく必要があります。
DOS ベースを使用している場合は、Babun のようなものをインストールするだけで、最小限の Linux 仮想マシンとターミナル エミュレーターを簡単に作成できます。
空のフォルダにシミュレーションを設定する
1. 希望する文字列を含むテーブルを10個作成する
for i in $(echo table{01..10}.tex); do touch $i && echo "Standard errors in parentheses & col2 & col3 \\\\" > $i;done
2. 正規表現(Perl方言)を再帰的に使用して一致をリストする(検査目的)
grep -Prn 'Standard errors in parentheses'
3. PerlとGrepの出力を使用してテキストを再帰的に置換する
perl -i -pe 's/Standard errors in parentheses/whatever/g' $(grep -Prl 'Standard errors in parentheses')
ノート
ファイルに書き込む前に置換が正しく機能していることを確認したい場合は、ターミナルに書き込むことができます。
perl -pe 's/Standard errors in parentheses/whatever/g' $(grep -Prl 'Standard errors in parentheses')
または拡張子.bakでバックアップを保存する
perl -i.bak -pe 's/Standard errors in parentheses/whatever/g' $(grep -Prl 'Standard errors in parentheses')
私はここ数年間、専門的に大きなテキストを扱っており、さまざまな正規表現言語方言を試してきました。私の経験では、Perl が最も便利で受け入れられている方言です。
- 先読みと後読みは非常に便利です
- 非貪欲演算子 ( などのプレースホルダーを制御します) 。
*
たとえば?)
、 すべての の内容を検索したいのです\paragraph{...}
が、他の は\macro{}
同じ行にある可能性があります。 と入力するだけで、grep -Prn '\\paragraph\{.*?\}'
最初の で停止します}
。