
我將許多表輸入到 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,那麼採用以下範例中所示的方法可能適合您。dosub
設定一個名為 的 Lua 函數來執行字串替換操作,並提供 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 是最有用、最被接受的方言。
- 前瞻和回顧,非常有用
- 非貪婪運算子(馴服佔位符,例如
*
:?)
您想要查找所有 s 的內容\paragraph{...}
,但其他一些\macro{}
可能位於同一行。只需鍵入grep -Prn '\\paragraph\{.*?\}'
即可在第一個 處停止}
。