我希望在包文档中添加 LaTeX 代码高亮显示。通常我使用以下代码来高亮显示 LaTeX 代码(从classicthesis
文档中复制):
\usepackage{listings}
\definecolor{lightergray}{gray}{0.99}
\lstset{language=[LaTeX]TeX,
keywordstyle=\color{RoyalBlue},
basicstyle=\ttfamily,
commentstyle=\color{ForestGreen}\ttfamily,
stringstyle=\rmfamily,
showstringspaces=false,
breaklines=true,
frame=lines,
backgroundcolor=\color{lightergray},
flexiblecolumns=true,
escapeinside={(*}{*)},
% numbers=left,
numberstyle=\scriptsize, stepnumber=1, numbersep=5pt,
firstnumber=last,
}
\providecommand{\meta}[1]{$\langle${\normalfont\itshape#1}$\rangle$}
\lstset{morekeywords=%
{maketitle,cref,...
}
}
\lstnewenvironment{code}{\setkeys{lst}{columns=fullflexible,keepspaces=true}}{}
但是,我不知道如何将其应用于doc
。将verbatim
或更改macrocode
为我的code
环境会导致许多错误。
以下是我的文件代码.dtx
。我该怎么办?
% \iffalse meta-comment
%
% Copyright (C) 2021 by Jinwen XU
% -------------------------------
%
% This file may be distributed and/or modified under the conditions of the LaTeX
% Project Public License, either version 1.3c of this license or (at your option)
% any later version. The latest version of this license is in:
%
% http://www.latex-project.org/lppl.txt
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{mindflow.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{mindflow}
%<*package>
[2021/05/07 Mindflow environment]
%</package>
%
%<*driver>
\documentclass{article}
\usepackage{doc}
\usepackage[a4paper,top=1.2in,bottom=1.2in,left=1.6in,right=1.2in]{geometry}
\usepackage{titlesec}
\titleformat{\section}{\large\scshape\centering}{\thesection.}{.5em}{}
\titleformat{\subsection}{\large}{\thesubsection}{.5em}{}
\usepackage{titling}
\setlength{\droptitle}{-.5in}
\usepackage{enumitem}
\setlist{noitemsep}
\usepackage{mathpazo,newpxtext}
\useosf
\PassOptionsToPackage{mathlines}{lineno}
\usepackage[linenumber,rightmarker]{mindflow}
\usepackage{blindtext}
\usepackage{parskip}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
\DocInput{mindflow.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{197}
%
% \GetFileInfo{mindflow.dtx}
%
%
% \title{The {\normalfont\textsf{mindflow}} package}
% \author{\scshape Jinwen Xu}
% \date{\filedate}
%
% \maketitle
%
% \section{Introduction}
%
% The \textsf{mindflow} package provides you a way to write your ideas
% and annotations clearly. For exmaple, with the options
% \verb|linenumber| and \verb|rightmarker|, you will get:
% \begin{mindflow}
% What to write next:
% \begin{itemize}
% \item usage;
% \item some internal macros;
% \item an example maybe;
% \item the complete code.
% \end{itemize}
% \end{mindflow}
%
% \section{Usage}
%
% \subsection{Initialization}
% Simply load the package with
% \begin{verbatim}
% \usepackage{mindflow}
% \end{verbatim}
%
% \vspace{-\baselineskip}
% There are a few options available, which are listed in the next subsection.
%
% \subsection{Options}
% By default, the environment has no line numbers or markers. You can use the following options:
%
% \begin{tabular}{ll}
% \verb|linenumber| & Same as \verb|leftlinenumber|\\
% \verb|leftlinenumber| & Enable left line numbers\\
% \verb|rightlinenumber| & Enable right line numbers\\
% \verb|leftmarker| & Enable left marker, by default it is a ``\verb|*|''\\
% \verb|rightmarker| & Enable right marker, by default it is a ``\verb|*|''\\
% \verb|incolumn| or \verb|twocolumn| & The separation line would fit in the column \\&(\emph{automatically applied in two-column documents})\\
% \verb|off| & Hide all the \verb|mindflow| environments
% \end{tabular}
%
% \DescribeEnv{\{mindflow\}}
% Then you can use the \verb|mindflow| environment as
% \begin{verbatim}
% \begin{mindflow}
% ...
% \end{mindflow}
% \end{verbatim}
% \vspace{-\baselineskip}
%
% \subsection{About the line numbers}
% \linenumbers
%
% You can also add line numbers to the other part of your document in the usual
% way. For example, line numbers have been turned on for this paragraph with
% \verb|\linenumbers|.
%
% \begin{mindflow}
% Line numbers within the \textsf{mindflow} environments are independent from
% those of the main text.
% \end{mindflow}
% (\emph{I recommend that you put an empty line before and after the mindflow
% environments, i.e.~make the \emph{\textsf{mindflow}} environment a paragraph
% of its own. This way, your paragraph structure won't get changed when you use
% the} \verb|off|\emph{ option.})
%
% \nolinenumbers
%
% The feature of line numbering is provided by the package \textsf{lineno}. You
% can read its documentation to learn more about it. Notice that, by default,
% displayed equations are not numbered. You can add the following line before
% \verb|\usepackage{mindflow}|:\\[.5\baselineskip]
% \null\hspace{1.5em}{\small\verb|\PassOptionsToPackage{mathlines}{lineno}|}\\[.5\baselineskip]
% to enable the line numbers for displayed equations.
%
% \begin{mindflow}\vspace{-\baselineskip}
% \begin{align*}
% (a + b)^2 &= a^2 + 2ab + b^2\\
% (a + b)^3 &= a^3 + 3a^2b + 3ab^2 + b^3
% \end{align*}
% \end{mindflow}
%
%
% \section{Some \TeX{}nical details}
%
% \DescribeMacro{\mindflowTextFont}
% \DescribeMacro{\mindflowNumFont}
% The font for text, line numbers and markers within the \verb|mindflow|
% environments can be specified by redefining \verb|\mindflowTextFont|,
% \verb|\mindflowNumFont| and \verb|\mindflowMarkerFont|. By default,
% they are defined as:
% \begin{verbatim}
% \newcommand{\mindflowTextFont}{\normalfont\footnotesize}
% \newcommand{\mindflowNumFont}{\normalfont\scriptsize\ttfamily}
% \newcommand{\mindflowMarkerFont}{\normalfont\scriptsize\ttfamily}
% \end{verbatim}
% \vspace{-.5\baselineskip}
%
% \DescribeMacro{\mindflowLeft}
% \DescribeMacro{\mindflowRight}
% The left and right markers can be changed by redefining
% \verb|\mindflowLeft| and \verb|\mindflowRight|. Both have the default
% value as ``\verb|*|''.
%
% \DescribeMacro{\mindflowLineHeight}
% The height of the separation lines is a length macro named \verb|\mindflowLineHeight|,
% with default value \verb|0.4pt|. You can change this with \verb|\setlength|.
%
% \DescribeMacro{mindflowLine}
% \DescribeMacro{mindflowText}
% \DescribeMacro{mindflowNum}
% \DescribeMacro{mindflowMarker}
% And finally, the colors for the separation lines, text, line numbers and
% markers within the \verb|mindflow| environments are called
% \verb|mindflowLine|, \verb|mindflowText|, \verb|mindflowNum| and
% \verb|mindflowMarker|, respectively. By default, they have the same color as
% the context, with opacity 30\% for the separation lines, text and markers, and
% 8\% for the line numbers.
%
% \section{An example}
% With option \verb|leftlinenumber, rightlinenumber, leftmarker, rightmarker| and the
% following settings:
% \begin{verbatim}
% \colorlet{mindflowLine}{green!50!black}
% \colorlet{mindflowText}{blue!50!cyan}
% \colorlet{mindflowNum}{red!50!orange}
% \colorlet{mindflowMarker}{orange}
% \renewcommand{\mindflowTextFont}{\normalfont\footnotesize\sffamily\itshape}
% \renewcommand{\mindflowNumFont}{\normalfont\small\footnotesize\ttfamily}
% \renewcommand{\mindflowMarkerFont}{\normalfont\footnotesize\sffamily}
% \renewcommand{\mindflowLeft}{\hspace{1em}\(\succ\)}
% \renewcommand{\mindflowRight}{\(\prec\)\hspace{1em}}
% \setlength{\mindflowLineHeight}{1pt}
% \end{verbatim}
% \vspace{-.5\baselineskip}
% One gets:
% \makeatletter
% \@mindflow@leftmarkertrue
% \@mindflow@rightlinenumbertrue
% \makeatother
% \colorlet{mindflowLine}{green!50!black}
% \colorlet{mindflowText}{blue!50!cyan}
% \colorlet{mindflowNum}{red!50!orange}
% \colorlet{mindflowMarker}{orange}
% \renewcommand{\mindflowTextFont}{\normalfont\footnotesize\sffamily\itshape}
% \renewcommand{\mindflowNumFont}{\normalfont\small\footnotesize\ttfamily}
% \renewcommand{\mindflowMarkerFont}{\normalfont\footnotesize\sffamily}
% \renewcommand{\mindflowLeft}{\hspace{1em}\(\succ\)}
% \renewcommand{\mindflowRight}{\(\prec\)\hspace{1em}}
% \setlength{\mindflowLineHeight}{1pt}
% \begin{mindflow}
% \blindtext
% \end{mindflow}
%
% \StopEventually{}
%
% \section{Implementation}
% Below is the complete source code of this package.
%
% \begin{macrocode}
\RequirePackage{kvoptions}
\SetupKeyvalOptions{%
family = @mindflow,
prefix = @mindflow@
}
\DeclareBoolOption[false]{off} % Turn off mindflow
\DeclareBoolOption[false]{leftmarker} % Left marker
\DeclareBoolOption[false]{rightmarker} % Right marker
\DeclareBoolOption[false]{linenumber} % Left line numbers
\DeclareBoolOption[false]{leftlinenumber} % Left line numbers
\DeclareBoolOption[false]{rightlinenumber} % Right line numbers
\DeclareBoolOption[false]{twocolumn} % Two column
\DeclareBoolOption[false]{incolumn} % Separation line fits in the column
\ProcessKeyvalOptions*\relax
\if@mindflow@twocolumn
\@mindflow@incolumntrue
\fi
\if@mindflow@linenumber
\@mindflow@leftlinenumbertrue
\fi
%%================================
%% Initialization
%%================================
\RequirePackage{lineno}
\RequirePackage{xcolor}
\colorlet{mfSavedColor}{.}
\colorlet{mindflowLine}{mfSavedColor!30}
\colorlet{mindflowText}{mfSavedColor!30}
\colorlet{mindflowMarker}{mfSavedColor!30}
\colorlet{mindflowNum}{mfSavedColor!8}
\newcommand{\mindflowTextFont}{\normalfont\footnotesize}
\newcommand{\mindflowNumFont}{\normalfont\scriptsize\ttfamily}
\newcommand{\mindflowMarkerFont}{\normalfont\scriptsize\ttfamily}
\newcommand{\mindflowLeft}{*}
\newcommand{\mindflowRight}{*}
\newlength{\mindflowLineHeight}
\setlength{\mindflowLineHeight}{0.4pt}
%%================================
%% The mindflow environment
%%================================
\newif\ifLNturnsON
\newcommand*{\mfSepLine}{%
\parskip=0pt
\LNturnsONfalse%
\ifLineNumbers\LNturnsONtrue\fi\nolinenumbers%
\par\noindent\nopagebreak%
\if@mindflow@incolumn%
\makebox[\linewidth]{\rule{\linewidth}{\mindflowLineHeight}}%
\else%
\hspace*{-\paperwidth}%
\makebox[\linewidth]{\rule{4\paperwidth}{\mindflowLineHeight}}%
\fi%
\nopagebreak\par%
\ifLNturnsON\linenumbers\fi%
}
\newcounter{recordLN}
\newcounter{mfLN}
\setcounter{mfLN}{1}
\if@mindflow@off
\RequirePackage{verbatim}
\let\mindflow=\comment
\let\endmindflow=\endcomment
\else
\newenvironment{mindflow}
{%
\setcounter{recordLN}{\value{linenumber}}%
\setcounter{linenumber}{\value{mfLN}}%
\LNturnsONfalse%
\ifLineNumbers\LNturnsONtrue\fi\nolinenumbers%
\color{mindflowLine}\mfSepLine%
\mindflowTextFont\color{mindflowText}%
\linenumbers%
\renewcommand\makeLineNumber{%
\hss%
\if@mindflow@leftlinenumber%
\mindflowNumFont\color{mindflowNum}\LineNumber\hspace{1em}%
\fi%
\color{mindflowMarker}%
\if@mindflow@leftmarker%
\mindflowMarkerFont\mindflowLeft\hspace{1em}%
\fi%
\rlap{\hskip\textwidth%
\if@mindflow@rightmarker%
\hspace{1em}\mindflowRight%
\fi%
\if@mindflow@rightlinenumber%
\hspace{1em}%
\mindflowNumFont\color{mindflowNum}\LineNumber%
\fi%
}%
}%
}
{%
\par%
\vspace{-.5\baselineskip}\color{mindflowLine}\mfSepLine%
\ifLNturnsON\linenumbers\fi%
\setcounter{mfLN}{\value{linenumber}}%
\setcounter{linenumber}{\value{recordLN}}%
}
\fi
\ifdefined\linenomathpatch\else
\RequirePackage{amsmath}
\RequirePackage{etoolbox}
\newcommand*\linenomathpatch[1]{%
\cspreto{#1}{\linenomath}%
\cspreto{#1*}{\linenomath}%
\csappto{end#1}{\endlinenomath}%
\csappto{end#1*}{\endlinenomath}%
}
\newcommand*\linenomathpatchAMS[1]{%
\cspreto{#1}{\linenomathAMS}%
\cspreto{#1*}{\linenomathAMS}%
\csappto{end#1}{\endlinenomath}%
\csappto{end#1*}{\endlinenomath}%
}
\expandafter\ifx\linenomath\linenomathWithnumbers
\let\linenomathAMS\linenomathWithnumbers
\patchcmd\linenomathAMS{\advance\postdisplaypenalty\linenopenalty}{}{}{}
\else
\let\linenomathAMS\linenomathNonumbers
\fi
\linenomathpatch{equation}
\linenomathpatchAMS{gather}
\linenomathpatchAMS{multline}
\linenomathpatchAMS{align}
\linenomathpatchAMS{alignat}
\linenomathpatchAMS{flalign}
\fi
% \end{macrocode}
%
% \Finale
\endinput