在进行计算机计算时,科学记数法通常表示为 1.23e-12。在科学写作中,人们不赞成使用这种表示法,我们使用显式乘法和上标来表示幂,如 1.23×10⁻¹²。这导致 Excel 和 Word 之间出现障碍,我们无法再简单地复制粘贴或更好地嵌入结果。这反过来又导致我们的文档中出现本可避免的打字错误。
有没有办法自动完成这个过程?例如,在 LaTeX 中,有许多软件包(例如希尼奇) 定义了至少可以自动执行转换的宏,因此您可以键入\num{1.23e-12}
以获得排版良好的结果。我理想情况下希望避免所有手动步骤,从 Excel 电子表格中的数字转换为 Word 文档中相同数字的排版版本,例如\renewcommand{\myimportantresult}{\num{1.23e-12}}
在 LaTeX 中定义,然后在任何地方使用宏而不是数字。
我已经发现此方法重新格式化科学计数法,这归结为使用以下公式(在 A2 中转换 A1):
=LEFT(TEXT(A1;"0.00E+0");3) & "×10^" & RIGHT(TEXT(A1;"0.00E+0");3)
这个结果很接近,结果是 1.2×10^-12,但我不知道如何只为通过公式计算的单元格部分添加上标。此外,这些“额外”单元格只是为了显示,这让电子表格变得复杂。
更困难的是,方法我尝试将 Excel 中的值链接到 Word(粘贴链接),但在我使用的 Mac 版 Office 2016 中似乎不起作用。
答案1
答案2
我不知道这是否是一个漂亮的代码,但它对我来说很有用。
Sub SciNotation()
' run for selected cells (cells that contain numbers)
For Each celula In Selection.Cells
crit = True ' set a crit varaible as true
txt = celula.Range.Text ' get the text to be corrected
m0 = 0 ' counter of the position of E+ or E-
If InStr(txt, "E+") > 0 Then
m0 = InStr(txt, "E+")
txt = Replace(txt, "E+", "*10+") ' replace E+ by 10+
ElseIf InStr(txt, "E-") > 0 Then
m0 = InStr(txt, "E-") ' replace E+ by 10-
txt = Replace(txt, "E-", "*10-")
Else
crit = False ' crit = False to go to next iteration
End If
' If found scientific notation to be corrected
If crit Then
n = Len(txt) ' get the number of characters of x
Set org = celula.Range ' set variable org as a range
org.Text = txt ' insert text
org.Text = Replace(org.Text, vbCr, "") ' remove last character (an undesirable enter)
For m = m0 + 3 To n - 1
org.Characters(m).Font.Superscript = True 'set superscript
Next
End If
Next
End Sub
答案3
Sub scientificnotation()
' put in general form
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "([0-9.]@)E([-+0-9]@)([!0-9])"
.Replacement.Text = "\1##×10##\2##\3"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
' take out leading 0 exponents
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "##+0"
.Replacement.Text = "##+"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' take out + exponents
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "##+"
.Replacement.Text = "##"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' take out leading 0 exponents for negative numbers
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "##-0"
.Replacement.Text = "##-"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' free up ×10
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "##×10##"
.Replacement.Text = "×10##"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' elevate exponents
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
.Superscript = True
.Subscript = False
End With
With Selection.Find
.Text = "##([-+0-9]@)##"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub