我有一个 C 项目,正在用 latex 记录。它具有 doxygen 注释(如那些/** bla */
/**< blab */
/// bla blub
///< blubber
)。我使用 listings 包将部分代码包含到我的文档中。有什么方法可以将 doxygen 代码包含在我的 C 代码中,处理\lstinputlisting
导入并让其删除 doxygen 注释,同时保留正常注释(// fizz
/* buzz */
)(请注意,\inputCCode
和\inputCCodeRanges
命令是自定义的,但提供美化的\lstinputlisting
命令)?我也可以使用某种预处理工具,但如果可以让 listings 包一次性完成整个工作,那就更好了……
考虑一下我的 MWE:
%file main.tex
\documentclass[a4paper,10pt]{report}
\usepackage[english,ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{ifthen}
\usepackage{xstring}
\usepackage{xcolor}
\input{programmingLanguagesFormating}
\begin{document}
\section{some title}
Some text that refers some code included with explicit line numbers
\inputCCode{1}{3}{some code to be refered to }{code.h}
Some text that refers some code that is included with the ranged include (solutions shall work with this as well)
\inputCCodeRanges{1-2,5-5,7-7}{Ranges of code that will explain the world}{code.h}
\end{document}
出于显而易见的原因(提示:长度),我将编程语言(在我的情况下是 C)的风格定义外包了。
%file programmingLanguagesFormating.tex
\definecolor{CComment}{rgb}{0.13,0.545,0.13}
\definecolor{CKeyword}{rgb}{0,0,1}
\definecolor{CString}{rgb}{0.627,0.125,0.941}
\lstdefinestyle{Cstyle}
{
language=C,
tabsize=4,
basicstyle=\ttfamily,
columns=flexible,
breaklines=true,
numbers=left,
showstringspaces=true,
rulecolor=\color{black},
numberstyle=\tiny\color{black},
stringstyle=\color{CString},
keywordstyle=\bfseries\color{CKeyword},
commentstyle=\itshape\color{CComment},
frame=line,
title=\lstname,
captionpos=b,
}
\lstnewenvironment{C}
{\lstset{ language=C, style=CStyle}}
{}
\newcommand{\inputCCode[5]}{\lstinputlisting[language=C,style=Cstyle,firstline=#2, lastline=#3,firstnumber=#2, caption={[#4]#4\hfill\\({\ifthenelse{\equal{#2}{#3}}{Line #2}{Lines #2~--~#3}} in File #5)}]{#5}}
\makeatletter % taken from https://tex.stackexchange.com/questions/110187/listings-line-numbers-that-match-the-linerange-specification
\makeatletter
\def\lst@MSkipToFirst{%
\global\advance\lst@lineno\@ne
\ifnum \lst@lineno=\lst@firstline
\def\lst@next{\lst@LeaveMode \global\lst@newlines\z@
\lst@OnceAtEOL \global\let\lst@OnceAtEOL\@empty
\ifnum \c@lstnumber>0
\vspace{2 mm}
\fi
\lst@InitLstNumber % Added to work with modified \lsthk@PreInit.
\lsthk@InitVarsBOL
\c@lstnumber=\numexpr-1+\lst@lineno % this enforces the displayed line numbers to always be the input line numbers
\lst@BOLGobble}%
\expandafter\lst@next
\fi}
\makeatother
\newcommand{\inputCCodeRanges[4]}{\lstinputlisting[language=C,style=Cstyle,linerange={#2}, caption={[#3]#3\hfill\\({\IfSubStr{#2}{,}{Lineranges #2}{Linerange #2}} in File #4)}]{#4}}
最后让文件code.h
成为:
// some C-Programming includes (this is a comment of the kind i need to keep)
#include <stdio.h>
#include <math.h>
/// \brief This is a demo function that is not even implemtented (this comment should go away (doxygen stlye))
void wizzBangGizmoAlgorithm(int magicParameter); ///< this comment explains the meaning of magicParameter and shall go away as well
int fruityWidget(void);
and
无论是谁,只要能够用或任何其他任意字符串替换范围标题中除最后一个逗号之外的所有逗号,就会获得额外的荣誉。