如何用注释排版编程代码语法?

如何用注释排版编程代码语法?

如果可能的话,我想知道如何在 latex/tikz 上在我的电子书上添加类似内容(环境和图表)。

多谢!!!

如何在 latex 或 tickz 上执行此操作?

我试图实现的代码:

\documentclass[11pt,fleqn]{book} 

\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,a4paper]{geometry} % Page margins

\usepackage{xcolor} % Required for specifying colors by name
\definecolor{ocre}{RGB}{243,102,25} % Define the orange color used for highlighting throughout the book

% Font Settings
\usepackage{avant} % Use the Avantgarde font for headings
%\usepackage{times} % Use the Times font for headings
\usepackage{mathptmx} % Use the Adobe Times Roman as the default text font together with math symbols from the Sym­bol, Chancery and Com­puter Modern fonts

\usepackage{microtype} % Slightly tweak font spacing for aesthetics
\usepackage[utf8]{inputenc} % Required for including letters with accents
\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs



% Index
\usepackage{calc} % For simpler calculation - used for spacing the index letter headings correctly
\usepackage{makeidx} % Required to make an index
\makeindex % Tells LaTeX to create the files required for indexing

\usepackage{titlesec} % Allows customization of titles

\usepackage{graphicx} % Required for including pictures
\graphicspath{{Pictures/}} % Specifies the directory where pictures are stored

\usepackage{lipsum} % Inserts dummy text

\usepackage{tikz} % Required for drawing custom shapes

\usepackage[brazil]{babel} % English language/hyphenation

\usepackage{enumitem} % Customize lists
\setlist{nolistsep} % Reduce spacing between bullet points and numbered lists

\usepackage{booktabs} % Required for nicer horizontal rules in tables

\usepackage{eso-pic} % Required for specifying an image background in the title page

%----------------------------------------------------------------------------------------
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % for normal

% Custom colors - http://get-software.net/info/colour/chroma/chroma.pdf
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{Keywords}{rgb}{0,0,1}
\definecolor{functions}{rgb}{0.75,0,0.5}
\definecolor{boolean}{rgb}{1,0.4,0}

%\usepackage{listings}
\usepackage[procnames]{listings}

% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\ttm,
otherkeywords={self},             % Add keywords here
keywordstyle=\ttb\color{deepblue},
%% keywords
emph={MyClass,__init__},          % Custom highlighting
emphstyle=\ttb\color{deepred},    % Custom highlighting style
%% keywords
stringstyle=\color{deepgreen},
frame=tb,                         % Any extra options here
%% keywords
emph={[2]or,and,not,def,for,in,if},                 % Custom highlighting
emphstyle=[2]\ttb\color{boolean},       % Custom highlighting style
%% keywords
emph={[3]input,print,range,int,float,str},                  % Custom highlighting
emphstyle=[3]\ttb\color{functions},     % Custom highlighting style
%%
procnamekeys={def,class},
procnamestyle=\color{blue}\ttb,
%%
commentstyle=\color{red}\ttm,
numbers=left,
numberstyle=\footnotesize,
numbersep=1em,
showstringspaces=false,            %
%%
rulecolor=\color{ocre}, % Cor da linha de borda
framexleftmargin=1mm, framextopmargin=1mm,
}}


% Python environment
\lstnewenvironment{python}[1][]
{
\pythonstyle
\lstset{#1}
}
{}

% Python for external files
\newcommand\pythonexternal[2][]{{
\pythonstyle
\lstinputlisting[#1]{#2}}}

% Python for inline
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}

%----------------------------------------------------------------------------------------
%----------------------------------------------------------------------------------------


\begin{document}

\begin{python}[backgroundcolor=\color{black!5}]
Python sintax: test of arguments

print('bla bla',234)

\end{python}

\end{document}

结果:

在此处输入图片描述

答案1

一个选择是使用tcolorbox包及其listings库:

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{fixltx2e}
\tcbuselibrary{listings}

\definecolor{color1}{RGB}{244,243,224}
\definecolor{color2}{RGB}{31,174,174}

\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{Keywords}{rgb}{0,0,1}
\definecolor{functions}{rgb}{0.75,0,0.5}
\definecolor{boolean}{rgb}{1,0.4,0}

\tcbset{listing engine=listings}

\newtcblisting{mybox}[1][]{%
  colback=yellow!5,
  colframe=color2,
  colbacktitle=color1,
  coltitle=black,
  listing only,
  title={\parbox[t]{2cm}{\textit{Syntax:}}\parbox[t]{\dimexpr\linewidth-2cm\relax}{#1}},
  fonttitle=\ttfamily,
  listing options={
    language=Python,
    basicstyle=\small\ttfamily,
    keywordstyle=\ttfamily\color{deepblue},
    stringstyle=\color{deepgreen},
    commentstyle=\ttfamily\color{red},
    showstringspaces=false,
    columns=fullflexible
  }
}

\begin{document}

\begin{mybox}[print()\\print(value\textsubscript{1},value\textsubscript{2},\ldots,value\textsubscript{n})]
print("The answer is", 6 + 7, "!" )
\end{mybox}

\end{document}

在此处输入图片描述

根据实际需要,另一种选择是使用“常规” tcolorbox;使用tikzmarktikz在指定元素处放置一些标记,然后使用这些标记绘制连接线:

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{fixltx2e}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\definecolor{color1}{RGB}{244,243,224}
\definecolor{color2}{RGB}{31,174,174}

\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{Keywords}{rgb}{0,0,1}
\definecolor{functions}{rgb}{0.75,0,0.5}
\definecolor{boolean}{rgb}{1,0.4,0}

\lstset{
    language=Python,
    basicstyle=\small\ttfamily,
    keywordstyle=\ttfamily\color{deepblue},
    stringstyle=\color{deepgreen},
    commentstyle=\ttfamily\color{red},
    showstringspaces=false,
    columns=fullflexible,
    escapeinside=||,
}

\newcommand\Text[2][0.5\linewidth]{%
  \parbox[t]{#1}{#2}%
}

\newtcolorbox{mybox}[1][]{%
  colback=yellow!5,
  colframe=color2,
  colbacktitle=color1,
  coltitle=black,
  title={\parbox[t]{2cm}{\textit{Syntax:}}\parbox[t]{\dimexpr\linewidth-2cm\relax}{#1}},
  fonttitle=\ttfamily
}

\begin{document}

\begin{mybox}[print()\\print(value\textsubscript{1},value\textsubscript{2},\ldots,value\textsubscript{n})]

\hfill\Text[.6\linewidth]{All arguments are optional. If no arguments are given, a blank\tikzmark{a} line is printed.}

\vspace{1cm}

\begin{lstlisting}
               print|\tikzmark{b}|("The |\tikzmark{d}|answer is", 6 |\tikzmark{e}|+ 7, "|\tikzmark{f}|!")|\tikzmark{c}|
\end{lstlisting}

\vspace{1cm}

\Text{The values to\tikzmark{g} be printed, one after the other, separated by a blank space}

\end{mybox}

\begin{tikzpicture}[remember picture,overlay,red!80!black]
\draw 
  ([yshift=8pt,xshift=4pt]pic cs:b) --+(0,10pt) 
  -| coordinate[near start] (aux) ([yshift=8pt,xshift=-4pt]pic cs:c);
\draw (aux) -- ([yshift=-8pt,xshift=-4pt]pic cs:a);
\foreach \i in {d,e,f}
  \draw ([yshift=10pt]pic cs:g) -- ([yshift=-5pt]pic cs:\i);
\end{tikzpicture}

\end{document}

在此处输入图片描述

第二种方法也可以使用mdframed

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{fixltx2e}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\definecolor{color1}{RGB}{244,243,224}
\definecolor{color2}{RGB}{31,174,174}

\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{Keywords}{rgb}{0,0,1}
\definecolor{functions}{rgb}{0.75,0,0.5}
\definecolor{boolean}{rgb}{1,0.4,0}

\lstset{
    language=Python,
    basicstyle=\small\ttfamily,
    keywordstyle=\ttfamily\color{deepblue},
    stringstyle=\color{deepgreen},
    commentstyle=\ttfamily\color{red},
    showstringspaces=false,
    columns=fullflexible,
    escapeinside=||,
}

\newcommand\Text[2][0.5\linewidth]{%
  \parbox[t]{#1}{#2}%
}

\newcommand\Title[1]{%
\parbox[t]{2cm}{\textit{Syntax:}}\parbox[t]{\dimexpr\linewidth-2cm\relax}{#1}%
}

\newmdenv[
  roundcorner=6pt,
  backgroundcolor=yellow!5,
  linecolor=color2,
  frametitlefont=\normalfont\ttfamily,
  frametitlerule=true,
  frametitlebackgroundcolor=color1,
]{mybox}

\begin{document}

\begin{mybox}[frametitle=\Title{print()\\print(value\textsubscript{1},value\textsubscript{2},\ldots,value\textsubscript{n})}]

\hfill\Text[.6\linewidth]{All arguments are optional. If no arguments are given, a blank\tikzmark{a} line is printed.}

\vspace{1cm}

\begin{lstlisting}
               print|\tikzmark{b}|("The |\tikzmark{d}|answer is", 6 |\tikzmark{e}|+ 7, "|\tikzmark{f}|!")|\tikzmark{c}|
\end{lstlisting}

\vspace{1cm}

\Text{The values to\tikzmark{g} be printed, one after the other, separated by a blank space}

\end{mybox}

\begin{tikzpicture}[remember picture,overlay,red!80!black]
\draw 
  ([yshift=8pt,xshift=4pt]pic cs:b) --+(0,10pt) 
  -| coordinate[near start] (aux) ([yshift=8pt,xshift=-4pt]pic cs:c);
\draw (aux) -- ([yshift=-8pt,xshift=-4pt]pic cs:a);
\foreach \i in {d,e,f}
  \draw ([yshift=10pt]pic cs:g) -- ([yshift=-5pt]pic cs:\i);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容