解决latex在使用lstlisting环境时的Undefined control sequence.错误
错误描述,如题,Undefined control sequence. \begin{lstlisting},查了不少的资料,起始就是一句话,缺了宏包的导入。先看代码:
\documentclass[11pt,a4paper]{ctexart} \usepackage{listings} %插入代码要引入的宏包 \author{gsc} \title{sample} \lstset{ columns = fixed, basicstyle = \linespread{1.2} \ttfamily, % 设置行距,字体 %numbers = left, % 在左侧显示行号 numberstyle = \tiny \color{gray}, % 设定行号格式 keywordstyle = \bfseries \color[RGB]{240,40,255}, % 设定关键字颜色 numberstyle = \footnotesize \color{darkgray}, commentstyle = \color[RGB]{0,96,96}, % 设置代码注释的格式 stringstyle = \color[RGB]{128,128,0}, % 设置字符串格式 frame = single, % 不显示背景边框 backgroundcolor = \color[RGB]{205,245,244}, % 设定背景颜色 showstringspaces = false, % 不显示字符串中的空格 language=Tex % 设置语言 } \begin{document} \begin{lstlisting}[language=tex] \begin{document} 坚持使用\LaTeX ,你会爱不释手! \end{document} \end{lstlisting} 坚持使用\LaTeX ,你会爱不释手! \end{document}
编译时就会出现各种问题,后来就添加了一行导入宏包\usepackage{xcolor},就解决了问题,记录下来。
正确代码:
\documentclass[11pt,a4paper]{ctexart} \usepackage{xcolor} \usepackage{listings} %插入代码要引入的宏包 \author{gsc} \title{sample} \lstset{ columns = fixed, basicstyle = \linespread{1.2} \ttfamily, % 设置行距,字体 %numbers = left, % 在左侧显示行号 numberstyle = \tiny \color{gray}, % 设定行号格式 keywordstyle = \bfseries \color[RGB]{240,40,255}, % 设定关键字颜色 numberstyle = \footnotesize \color{darkgray}, commentstyle = \color[RGB]{0,96,96}, % 设置代码注释的格式 stringstyle = \color[RGB]{128,128,0}, % 设置字符串格式 frame = single, % 不显示背景边框 backgroundcolor = \color[RGB]{205,245,244}, % 设定背景颜色 showstringspaces = false, % 不显示字符串中的空格 language=Tex % 设置语言 } \begin{document} \begin{lstlisting}[language=tex] \begin{document} 坚持使用\LaTeX ,你会爱不释手! \end{document} \end{lstlisting} 坚持使用\LaTeX ,你会爱不释手! \end{document}
搞定了在latex中展示latex代码
人就像是被蒙着眼推磨的驴子,生活就像一条鞭子;当鞭子抽到你背上时,你就只能一直往前走,虽然连你也不知道要走到什么时候为止,便一直这么坚持着。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2023-01-20 在线编辑键盘布局
2021-01-20 manjaro安裝anaconda
2021-01-20 让你的Manjaro也具有生产力
2021-01-20 安装linux找不到硬盘
2021-01-20 OpenGL实践之--代码重现
2021-01-20 让Win10字体变得像苹果电脑那样细腻
2021-01-20 Shell编程入门