问题描述
有谁知道在 Microsoft Word 文档中显示代码的方法,可以保留颜色和格式?优选地,该方法也将不显眼且易于更新.
Does anyone know a way to display code in Microsoft Word documents that preserves coloring and formatting? Preferably, the method would also be unobtrusive and easy to update.
我试图将代码作为常规文本包含在内,这看起来很糟糕,并且在编辑常规文本时会造成妨碍.我还尝试将对象、写字板文档和文本框插入到文档中,然后将代码放入这些对象中.代码看起来好多了,并且在编辑文本的其余部分时更容易避免.然而,这些对象只能跨越一个页面,当需要添加多页代码时,这使得编辑成为一场噩梦.
I have tried to include code as regular text which looks awful and gets in the way when editing regular text. I have also tried inserting objects, a WordPad document and Text Box, into the document then putting the code inside those objects. The code looks much better and is easier to avoid while editing the rest of the text. However, these objects can only span one page which makes editing a nightmare when several pages of code need to be added.
最后,我知道有更好的编辑器/格式可以毫无问题地处理这个问题,但我一直在使用 MS word.
Lastly, I know that there are much better editors/formats that have no problem handling this but I am stuck working with MS word.
推荐答案
对我来说,这是在 word 中添加代码的最佳方法:
Here is the best way, for me, to add code inside word:
- 转到
Insert
标签,Text
部分,点击Object
按钮(它在右边) - 选择
OpenDocument Text
这将打开一个新的嵌入 Word 文档 - 将您的代码从 Visual Studio/Eclipse 复制并粘贴到此嵌入的单词页面中
- 保存并关闭
- Go to
Insert
tab,Text
section, clickObject
button (it's on the right) - Choose
OpenDocument Text
which will open a new embedded word document - Copy and paste your code from Visual Studio / Eclipse inside this embedded word page
- Save and close
优势
结果看起来很不错.以下是这种方法的优点:
Advantages
The result looks very nice. Here are the advantages of this method:
- 代码保持原来的布局和颜色
- 代码与文档的其余部分分开,就像图片或图表一样
- 不会在代码中突出显示拼写错误(这很酷!)
而且只需几秒钟.
这篇关于如何以 MS Word 保留格式和语法高亮显示代码片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!