如何从文本中删除特定的乳胶命令及其后面的右括号,但将文本保留在括号内/
毫无意义的例子:
We \edit{Introduce a} model for analyzing \emph{data} from various
experimental designs, \edit{such as paired or \url{http://www/}
longitudinal; as was done 1984 by NN \cite{mycitation} and by NNN
\cite{mycitation2}}.
输出:
We Introduce a model for analyzing \emph{data} from various
experimental designs, such as paired or \url{http://www/}
longitudinal; as was done 1984 by NN \cite{mycitation} and by NNN
\cite{mycitation2}.
PS。我在我的 tex 文件中引入了许多小编辑。我希望这些编辑被突出显示,以便我的合作者可以看到它们。但之后我想删除所有突出显示并将文本发送给审阅者。
答案1
新答案
因为听起来你想修改你的源代码,所以我写了一个新的非答案。
这不是您想在 TeX 中完成的事情。您可能想尝试为其编写一个正则表达式,但使用真正的正则表达式不会成功。
更好的解决方案可能是指示编辑器执行您想要的操作。例如,在 Vim 中,将光标放在 上\
,\edit{...}
输入5xma%x`ax
会删除前 5 个字符(\edit
),标记当前位置({
),移动到匹配的}
,删除它,移回标记的位置并删除它。与/\\edit{
VIM 宏结合使用可以非常轻松地删除它们。
归功于贾斯汀·史密斯的回答对于ma%x`ax
。 (或者如编辑所述,%x``x
与 具有相同的效果ma%x`ax
。)
旧答案
这个答案假设你只是想\edit
不做任何事情,但现在很明显这不是你想要的。
您想要(重新)定义\edit
仅输出其参数而不进行任何改变。
\renewcommand\edit[1]{#1}
这是一个例子。
\documentclass{article}
\usepackage{xcolor}
\usepackage{url}
\newcommand\edit[1]{\textcolor{blue}{#1}}
%\renewcommand\edit[1]{#1}
\begin{document}
We \edit{Introduce a} model for analyzing \emph{data} from various
experimental designs, \edit{such as paired or \url{http://www/}
longitudinal; as was done 1984 by NN \cite{mycitation} and by NNN
\cite{mycitation2}}.
We Introduce a model for analyzing \emph{data} from various
experimental designs, such as paired or \url{http://www/}
longitudinal; as was done 1984 by NN \cite{mycitation} and by NNN
\cite{mycitation2}.
\end{document}
如果我取消注释该\renewcommand
行,我会得到以下内容。
在这种情况下,这两个段落是相同的。
答案2
这是一个基于Python的解决方案,不够简洁,但嵌套命令效果很好。
def command_remove(tex_in, keywords):
# Romove command with curly bracket
# keywords: "hl textbf" mean removing \hl{} and \textbf{}
pattern = '\\\\(' + keywords.replace(' ', '|') + '){'
commands = re.finditer(pattern, tex_in)
idxs_to_del = [] # The index of }
for command in commands:
stack = 0
current_loc = command.span()[1]
while not (tex_in[current_loc] == '}' and stack == 0):
if tex_in[current_loc] == '}':
stack = stack - 1
if tex_in[current_loc] == '{':
stack = stack + 1
current_loc = current_loc + 1
idxs_to_del.append(current_loc)
idxs_to_del = sorted(idxs_to_del, reverse=True) # sort
tex_list = list(tex_in)
for idx in idxs_to_del:
tex_list.pop(idx) # remove }
tex_out = ''.join(tex_list)
tex_out = re.sub(pattern, '', tex_out) # remove \xxx{
return tex_out
它通过正则表达式定位目标命令,然后使用堆栈定位右括号的位置。tex_out = command_remove(tex_in, "revise textbf")
对于tex_in
:
\hl{Can you} \revise{can a \textbf{can} as a \emph{canner} can} can a can?
我们将得到tex_out
:
\hl{Can you} can a can as a \emph{canner} can can a can?
更多详细信息,即命令行运行,请参阅Latex_command_remove。
答案3
虽然是一篇旧帖子,但它可能仍然具有相关性并且有趣。
changes
以下是使用包的方法卡坦。
首先,让我们看一下原始代码片段,使用选项将\edit{}
语句替换为来自的代码。我在末尾添加了一个,可以通过选项进一步调整。changes
[draft]
\listofchanges
\documentclass[10pt]{article}
\usepackage{hyperref}
% ~~~ this one is newly introduced here ~~~~~
\usepackage[draft]{changes}% <<< draft
% ~~~ to define ID's and assign colors ~~~~
\definechangesauthor[color=orange]{DEN}\definechangesauthor[color=red]{ME}
\begin{document}
% ~~~ replacing \edit{ } from original ~~~~
We
\added[id=DEN]{Introduce a}
model for analyzing \emph{data} from various experimental designs, such as paired or \url{http://www/} longitudinal; as was done 1984 by NN \cite{mycitation}
\deleted[id=ME,comment={just to show this feature}]{and by NNN\cite{mycitation2}}
.
\vspace{2\baselineskip}
\listofchanges
\end{document}
~~
最后,让我们看看当切换到[final]
包的选项时,相同的代码会发生什么:它保留所有更改的代码,但根据需要重新处理输出 pdf。
\documentclass[10pt]{article}
\usepackage{hyperref}
% ~~~ this one is newly introduced here ~~~~~
\usepackage[final]{changes}% <<< switching to final
% ~~~ to define ID's and assign colors ~~~~
\definechangesauthor[color=orange]{DEN}\definechangesauthor[color=red]{ME}
\begin{document}
% ~~~ replacing \edit{ } from original ~~~~
We
\added[id=DEN]{Introduce a}
model for analyzing \emph{data} from various experimental designs, such as paired or \url{http://www/} longitudinal; as was done 1984 by NN \cite{mycitation}
\deleted[id=ME,comment={just to show this feature}]{and by NNN\cite{mycitation2}}
.
\vspace{2\baselineskip}
\listofchanges
\end{document}
~~
要删除所有更改标记,请应用script pyMergeChanges.py
,如手册中所述。