Kapitel/Chapter 14

Einführung in LaTeX / Introduction into LaTeX

14.2

Reading and writing files

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.82]{beramono}
\usepackage{blindtext}
\usepackage{fancyvrb}
\usepackage{listings}
\newlength\CodeWidth
\makeatletter
\newenvironment{ShowCode}[2][]
{\lstset{#1}\VerbatimEnvironment%
\global\CodeWidth=#2%
\begin{VerbatimOut}{\jobname.vrb}}
{\end{VerbatimOut}%
\noindent%
\minipage[t]{\CodeWidth}\vspace{0pt}\input{\jobname.vrb}\endminipage%
\minipage[t]{\dimexpr\linewidth-\CodeWidth-2\fboxsep-2\fboxrule\relax}
\vspace{0pt}\lstinputlisting{\jobname.vrb}\endminipage}
\makeatother
\begin{document}

\begin{ShowCode}[language={[LaTeX]TeX},frame=single,columns=fixed,
  basicstyle=\small\ttfamily,breaklines,postbreak=\mbox{$\hookrightarrow$},
  keywordstyle=\bfseries]{0.6\linewidth}
\begin{itemize}
  \item Thisisaitemwithnosenseinitstext
  \item anotherone
\end{itemize}
\end{ShowCode}

\end{document}

Writing files with umlauts

\documentclass[border=5mm]{standalone}
\usepackage[utf8]{inputenx}
\usepackage{fancyvrb}
\begin{document}

\begin{VerbatimOut}[codes={\InputEncoding{verbatim}}]{test.tex}
==äööÄÄÄÖÖ==
\end{VerbatimOut}

\VerbatimInput[]{test}
\end{document}

Sinnvoller ist die Anwendung von lualatex:

% !TEX lualatex
\documentclass[a5paper,12pt]{article}
\usepackage{fancyvrb}
\begin{document}

\begin{VerbatimOut}{test.tex}
==äööÄÄÄÖÖ==
\end{VerbatimOut}

\VerbatimInput{test}
\end{document}

14.3

Special listings

% !TEX lualatex
\documentclass{article}
\usepackage{fontspec}
\usepackage[Scale=MatchLowercase,FakeStretch=0.82]{AnonymousPro}
\usepackage{pst-solides3d,lstlinebgrd,xcolor}
\pagestyle{empty}
\lstset{
    linebackgroundcolor={\ifodd\value{lstnumber}\color{black!20}\fi},
    linebackgroundsep=3pt,
    linebackgroundwidth={\dimexpr\linewidth + (\lst@linebgrdsep)*2 \relax},
    frame=lr,
    xleftmargin=15pt,
    xrightmargin=5pt,
    framexleftmargin=0pt,
    framexrightmargin=0pt,
    basicstyle=\ttfamily\small
}
\begin{document}

\begin{lstlisting}
\begin{pspicture}[solidmemory](-6,-4)(6,4)
\psset{viewpoint=30 0 15 rtp2xyz,Decran=30,lightsrc=viewpoint}
\psSolid[object=tore,r1=5,r0=1,ngrid=36 36,opacity=0.5,
    fillcolor=blue!30,action=none,name=Torus]%
%\axesIIID(4.5,4.5,0)(5,5,4)
\codejps{/R1 5 def /RL 0.01 def /R0 1.05 def /k 25 def}%
\defFunction[algebraic]{helix}(u,v)
    {(R1 + (R0 +RL*sin(u))*sin(k*v))*cos(v)-RL*cos(u)*sin(v)}
    {(R1 + (R0 +RL*sin(u))*sin(k*v))*sin(v)+RL*cos(u)*cos(v)}
    {(R0 + RL*sin(u))*cos(k*v)}
\psSolid[object=surfaceparametree,
       base=0 6.2831853 0 6.2831853,
       linecolor=blue,linewidth=0.01,fillcolor=yellow,
       ngrid=0.8 0.01,function=helix,action=none,name=Helix]%
\psSolid[object=fusion,base=Torus Helix,grid=false,opacity=0.5]
%\gridIIID[Zmin=-3,Zmax=3,showAxes=false](-2,2)(-2,2)
\end{pspicture}
\end{lstlisting}

\end{document}

Listings with tcolorbox

\documentclass[parskip=half-]{scrbook}
\usepackage[most]{tcolorbox}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2
}
% texdoc tcolorbox
% https://tex.stackexchange.com/questions/305387/adding-a-caption-to-a-tcolorbox-tcblisting
% https://tex.stackexchange.com/questions/343105/reverse-caption-number-and-text-order-in-special-listing
\renewcommand*\thelstnumber{\makebox[3em][r]{%
  \ifnum\value{lstnumber}<10 0\fi\the\value{lstnumber}}}
\AtBeginDocument{%
  \newtcblisting[blend into=listings]{PythonCode}[1][]{
    breakable,
    listing only,
    %colframe = blue!50!black,
    colback = backcolour,
    frame hidden,
    left=0mm,
    right=0mm,
    top=0mm,
    bottom=0mm,
    enhanced,
    sharp corners,
    listing remove caption=false,
    coltitle=black,
    attach boxed title to top center={yshift=\belowcaptionskip},
    boxed title style={colback=white, sharp corners, frame hidden},
    listing options={style=mystyle,language=Python},
    hbox,
    #1
  }
}
\begin{document}
\lstlistoflistings

\noindent\rule{\textwidth}{1mm}

\centering
\begin{PythonCode}[list text={A Python Program},title={A Python Function}]
import numpy as np

def incmatrix(genl1,genl2):
    m = len(genl1)
    n = len(genl2)
    M = None #to become the incidence matrix
    VT = np.zeros((n*m,1), int)  #dummy variable

    #compute the bitwise xor matrix
    M1 = bitxormatrix(genl1)
    M2 = np.triu(bitxormatrix(genl2),1)
    for i in range(m-1):
        for j in range(i+1, m):
            [r,c] = np.where(M2 == M1[i,j])
            for k in range(len(r)):
                VT[(i)*n + r[k]] = 1;
                VT[(i)*n + c[k]] = 1;
                VT[(j)*n + r[k]] = 1;
                VT[(j)*n + c[k]] = 1;
                if M is None:
                    M = np.copy(VT)
                else:
                    M = np.concatenate((M, VT), 1)
                VT = np.zeros((n*m,1), int)
    return M
\end{PythonCode}

\noindent\rule{\textwidth}{1mm}

\end{document}

Zurück zur Hauptseite/Back to main page
Copyright $Id: chapter14.html 2 2022-12-28 16:38:18Z voss $