Richtige Umgebung für einen Algorithmus schreiben

Antwort erstellen


Diese Frage dient dazu, das automatisierte Versenden von Formularen durch Spam-Bots zu verhindern.
Smilies
:D :) :( :o :shock: :? 8) :lol: :-x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen:

BBCode ist eingeschaltet
[img] ist eingeschaltet
[flash] ist ausgeschaltet
[url] ist eingeschaltet
Smilies sind eingeschaltet

Die letzten Beiträge des Themas

Ich habe die Datenschutzerklärung gelesen und bin damit einverstanden.

   

Wenn du eine Datei oder mehrere Dateien anhängen möchtest, gib die Details unten ein.

Ansicht erweitern Die letzten Beiträge des Themas: Richtige Umgebung für einen Algorithmus schreiben

von Bartman » Fr 21. Jun 2019, 00:04

Falls das Thema noch aktuell ist, möchte ich die folgenden Herangehensweisen vorschlagen.

Mit dem Paket tocbasic:
\documentclass[ngerman]{article}
\usepackage[utf8]{inputenc}% bei einer aktuellen TeX-Distribution verzichtbar
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{babel} 
\usepackage{tocbasic}
\usepackage{enumitem}
\usepackage{caption}
\usepackage{cleveref}

\DeclareNewTOC[
    name=Algorithmus,
    listname={Algorithmenverzeichnis},
    type=algorithm,
    float,
    counterwithin=section,
    atbegin=\captionsetup{singlelinecheck=off, labelfont=bf}
]{loa}

\newlist{steps}{enumerate}{1}
\setlist[steps]{label=Step~\arabic*:, leftmargin=*, nosep}

\begin{document}
\listofalgorithms

\section{Abschnitt}
Siehe \cref{algo:algorithmus1}

\begin{algorithm}[htbp]
    \caption{Beschriftung des Algorithmus}
    \label{algo:algorithmus1}
    \begin{steps}
    \item mache was
    \item mache was
    \item mach was und wenn das, dann geh zu S1
    \item Wenn das dann Ende, sonst gehe zu S1 und jetzt steht hier was ganz langes, was dann über zwei Zeilen geht und das soll nicht unter Step 4 stehen sondern genau unter dem Wenn
    \end{steps}
\end{algorithm}
\end{document}
Mit dem Paket algorithms:
\documentclass[ngerman]{article}
\usepackage[utf8]{inputenc}% bei einer aktuellen TeX-Distribution verzichtbar
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{babel} 
\usepackage{enumitem}
\usepackage{caption}
\usepackage{algorithm}
\usepackage{chngcntr}% siehe Kommentar zu inputenc
\usepackage{cleveref}

\floatname{algorithm}{Algorithmus}
\renewcommand{\listalgorithmname}{Algorithmenverzeichnis}

\counterwithin{algorithm}{section}

\newlist{steps}{enumerate}{1}
\setlist[steps]{label=Step~\arabic*:, leftmargin=*, nosep}


\begin{document}
\listofalgorithms

\section{Abschnitt}
Siehe \cref{algo:algorithmus1}

\begin{algorithm}
    \caption{Beschriftung des Algorithmus}
    \label{algo:algorithmus1}
    \begin{steps}
    \item mache was
    \item mache was
    \item mach was und wenn das, dann geh zu S1
    \item Wenn das dann Ende, sonst gehe zu S1 und jetzt steht hier was ganz langes, was dann über zwei Zeilen geht und das soll nicht unter Step 4 stehen sondern genau unter dem Wenn
    \end{steps}
\end{algorithm}
\end{document}

von Bernd_Maler » Fr 14. Jun 2019, 15:48

Vielen Dank für die schnelle Antwort, da habe ich mich schonmal durchgeklickt und etwas gefunden. Ich will es ja nicht so als pseudocode schreiben sondern einfach nur aufzahählen und alles schön unterinander haben.
Da soll kein If oder While drin sein. Einfach nur Text und mal hin und wieder eine Variable im Math Mode.
Jetzt habe ich programbox gefunden im package program und die macht schon ziemlich gut das was ich möchte.
Aber da kommt der Text auch unter dem Step2 und ein & kann man da nicht einbauen.
Es gibt leider keine weitere Beschreibung über diese Programbox, vielleicht kennt sich ja jemand damit aus ?

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}   
\usepackage[german]{babel}              
\usepackage{mathtools}                 %mathclap
\usepackage{multicol}                  %multirows & multicolumns
\usepackage{eso-pic}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{pgf}
\usetikzlibrary{arrows,automata,positioning}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{arrows.meta,bending}
\pgfarrowsdeclarecombine{twoto}{twoto}%
{to}{to}{to}{to}
\usetikzlibrary{decorations.text}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{bm}
\usepackage{xfrac}                %sfrac{}{}
\usepackage{amsmath}

\numberwithin{equation}{section}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
%\usepackage{gensymb}         %degree symbol etc. not compatible with mathabx
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{microtype}
%\usepackage{mathabx}                %nicer arrows
\usepackage{wasysym}
\usepackage{makeidx}
\usepackage{graphicx}                  %for scalebox
\usepackage{multirow}                  %fancy tricks in tables
\usepackage{ragged2e}   % better raggedright
\usepackage{pgfplots}                  % diagramms
\usepackage{chngcntr}
\usepackage{sectsty}                  %section fonts
%\usepackage{MnSymbol}                 
\usepackage[normalem]{ulem}           %for better undelrine with wrapping
\usepackage{float}                     % for H in float positioning
\usepackage{fancyhdr}                  %fancy header
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0.5pt}
\fancyhead[LO,RE]{{\small \leftmark}}
\fancyfoot[C]{\thepage}
\usepackage[a4paper]{geometry}     %margins & layout  ,showframe
\usepackage{booktabs}              % Horizontal rules in tables (nicer)
\usepackage{array}                 % Text wraping in tables with alignment
\setcounter{MaxMatrixCols}{20}        % maximum of columns in pmatrix
\usepackage{caption}                   % to add source under captions
\PassOptionsToPackage{hyphens}{url}
\usepackage{hyperref}
%\hypersetup{
%    colorlinks,
%    citecolor=black,
%    filecolor=black,
%    linkcolor=black,
%    urlcolor=black
%}
\usepackage{enumitem}                  %lepsi itemize
\usepackage{rotating}
\usepackage{multicol}   
\usepackage{makeidx}
\usepackage{program}
\makeindex
% % % % % % % % % % % % % % % % % %
\setcounter{secnumdepth}{4}
\setcounter{tocdepth}{2}
\tolerance=1
\emergencystretch=\maxdimen
\hyphenpenalty=7000
\hbadness=7000
\let\cleardoublepage\clearpage
% % % % % % % % % % % % % % % %
\newcounter{thectrbox}
\newtheoremstyle{break}% name
{\topsep}%  Space above, empty = `usual value'
{\topsep}%   Space below
{}%  Body font
{}%          Indent amount (empty = no indent, \parindent = para indent)
{\bfseries}% Thm head font
{.}%         Punctuation after thm head
{ }%  Space after thm head: \newline = linebreak
{#1.#2~#3}%          Thm head spec
\theoremstyle{break}
\newtheorem{ctrbox}[thectrbox]{\thesection}
\newtheoremstyle{nbreak}% name
{\topsep}%  Space above, empty = `usual value'
{\topsep}%   Space below
{}%  Body font
{}%          Indent amount (empty = no indent, \parindent = para indent)
{\bfseries}% Thm head font
{.}%         Punctuation after thm head
{ }%  Space after thm head: \newline = linebreak
{#3}%          Thm head spec
\theoremstyle{nbreak}
\newtheorem{nctrbox}[]{}
% % % % % % % % % % % % % % % %
\setlist[itemize]{noitemsep,align=left,leftmargin=20pt,labelwidth=*}
\renewcommand{\labelitemi}{\textbullet}
\renewcommand{\labelitemii}{\textbullet}
\renewcommand{\labelitemiii}{\textbullet}
% % % % % % % % % % % % % % %
\renewcommand{\qedsymbol}{{\footnotesize $\blacksquare$}}
\makeatletter \renewenvironment{proof}[1][\proofname] {\par\pushQED{\qed}\normalfont\topsep6\p@\@plus6\p@\relax\trivlist\item[\hskip\labelsep\bfseries#1\@addpunct{.}]\ignorespaces}{\popQED\endtrivlist\@endpefalse} \makeatother
% % % % % % % % % % % % % % % % %
\renewcommand{\Im}{\mathrm{Im} \,} % image instead of imaginary part
\renewcommand{\tilde}{\widetilde}
\newcommand{\Arrow}{\! \! \! \! \rotatebox{90}{\makebox[0.02\textwidth]{\raisebox{-5pt}{\includegraphics[width=0.02\textwidth]{arrow.pdf}}}} \! \!\!}
% % % % % % % % % % % % % % % % % % Algebra befehle
\newcommand{\fix}{\mathrm{Fix}\,}
\newcommand{\sgn}{\mathrm{sgn}\,}
\newcommand{\id}{\mathrm{id}}
\newcommand{\Aut}{\mathrm{Aut}\,}
\newcommand{\coef}{\mathrm{coef}\,}
\newcommand{\Ker}{\mathrm{Ker}\,}
\newcommand{\ord}{\mathrm{ord}\,}
\newcommand{\Inv}{\mathrm{Inv}\,}
\newcommand{\xInv}[1]{#1\text{-}\mathrm{Inv}\,}
\newcommand{\nOrb}[1]{#1\text{-}\mathrm{Orb}\,}
\newcommand{\Orb}{\mathrm{Orb}\,}
\newcommand{\factor}[2]{\text{\raisebox{3pt}{$#1$}} \ \mathclap{\text{{\LARGE /}}} \ \text{\raisebox{-3pt}{$#2$}}}
\newcommand{\ifactor}[2]{\text{\raisebox{-3pt}{$#1$}} \ \mathclap{\text{{\LARGE \normalfont{\textbackslash}}}} \ \text{\raisebox{3pt}{$#2$}}}
\newcommand{\syl}[2]{\mathrm{Syl}_{#1}(#2)}
\newcommand{\ntlneq}{\,\raisebox{1pt}{$\lhd$}\hspace{-6pt}\raisebox{-4pt}{$\neq$}\,}
\renewcommand{\epsilon}{\varepsilon} % schoeneres Epsilon
\renewcommand{\b}[1]{\boldsymbol{#1}}
% % % % % % % % % % % % % % % % % % % % Ersatz für mnsymbol
\newcommand{\cupdot}{\uplus}
\newcommand{\bigcupdot}{\biguplus}
\renewcommand{\div}{\mid}
\newcommand{\ndiv}{\nmid}
% % % % % % % % % % % % % % % % % % % %
\newcommand{\pot}[1]{\mathfrak{P}\left(#1\right)}
\makeatletter
\newcommand*{\defeq}{\mathrel{\rlap{%
			\raisebox{0.3ex}{$\m@th\cdot$}}%
		\raisebox{-0.3ex}{$\m@th\cdot$}}%
	=}
\newcommand*{\defiff}{\mathrel{\rlap{%
			\raisebox{0.3ex}{$\m@th\cdot$}}%
		\raisebox{-0.3ex}{$\m@th\cdot$}}%
	\Longleftrightarrow}
\newcommand*{\bdefiff}{\Longleftrightarrow\mathrel{\rlap{%
			\raisebox{0.3ex}{$\m@th\cdot$}}%
		\raisebox{-0.3ex}{$\m@th\cdot$}}%
}
\makeatother
% % % % % % % % % % % % % % % % % % % % % INDEX
\usepackage{makeidx}
\makeindex
\begin{document}


\begin{ctrbox}[Algorithmus]
	
	\begin{programbox}
	\text{Step 1:   mache was} \newline
	\text{Step 2:   mache was} \newline
	\text{Step 3:   mach was und wenn das, dann geh zu S1} \newline
	\text{Step 4:   Wenn das dann Ende, sonst gehe zu S1 und jetzt steht hier was} \\ 
\text{	ganz langes, was dann über 2 Zeilen geht und das soll nicht unter Step 4 stehen} \\
	\text{ sondern genau unter dem Wenn} 
\end{programbox}

\end{ctrbox}


Dafür musste ich bloß das package program einbinden, aber bei mir steht das Wort Algorithmus jetzt ganz rechts und nicht mehr links.
Kann mir da jemand helfen ?

von Bartman » Fr 14. Jun 2019, 14:55

Sieh Dir das vorhandene Angebot zu dem Thema an, bevor Du anfängst, das Rad neu zu erfinden.

Richtige Umgebung für einen Algorithmus schreiben

von Bernd_Maler » Fr 14. Jun 2019, 14:05

Guten Morgen liebe Alle,

ich bin leider immer noch ein Anfänger und beim googeln habe ich nichts gefunden.
Ich würde gerne einen Algorithmus nieder schreiben. Das ganze soll so aussehen:
1.1 Algoritmus

Step 1: mache was
Step 2: mache was
Step 3: mach was und wenn das, dann geh zu S1
Step 4: Wenn das dann Ende, sonst gehe zu S1 und besonders wenn es etwas länger ist, soll die nächste Zeile unter dem "Wenn" seinund nicht unter Step 4

Da ich mehrere Algorithmen machen will soll er die auch fortlaufend zählen.
Also es soll wie bei mathematischen Sätzen Algorithmus 4.1 und Algorithmus 4.2 usw davor stehen, daher hatte ich an ctrbox gedacht, ist das richtig ?

Ich habe hier mal ein Minimalbeispiel:
\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}   
\usepackage[german]{babel}              
\usepackage{mathtools}                 %mathclap
\usepackage{multicol}                  %multirows & multicolumns
\usepackage{eso-pic}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{pgf}
\usetikzlibrary{arrows,automata,positioning}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{arrows.meta,bending}
\pgfarrowsdeclarecombine{twoto}{twoto}%
{to}{to}{to}{to}
\usetikzlibrary{decorations.text}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{bm}
\usepackage{xfrac}                %sfrac{}{}
\usepackage{amsmath}

\numberwithin{equation}{section}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
%\usepackage{gensymb}         %degree symbol etc. not compatible with mathabx
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{microtype}
%\usepackage{mathabx}                %nicer arrows
\usepackage{wasysym}
\usepackage{makeidx}
\usepackage{graphicx}                  %for scalebox
\usepackage{multirow}                  %fancy tricks in tables
\usepackage{ragged2e}   % better raggedright
\usepackage{pgfplots}                  % diagramms
\usepackage{chngcntr}
\usepackage{sectsty}                  %section fonts
%\usepackage{MnSymbol}                 
\usepackage[normalem]{ulem}           %for better undelrine with wrapping
\usepackage{float}                     % for H in float positioning
\usepackage{fancyhdr}                  %fancy header
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0.5pt}
\fancyhead[LO,RE]{{\small \leftmark}}
\fancyfoot[C]{\thepage}
\usepackage[a4paper]{geometry}     %margins & layout  ,showframe
\usepackage{booktabs}              % Horizontal rules in tables (nicer)
\usepackage{array}                 % Text wraping in tables with alignment
\setcounter{MaxMatrixCols}{20}        % maximum of columns in pmatrix
\usepackage{caption}                   % to add source under captions
\PassOptionsToPackage{hyphens}{url}
\usepackage{hyperref}
%\hypersetup{
%    colorlinks,
%    citecolor=black,
%    filecolor=black,
%    linkcolor=black,
%    urlcolor=black
%}
\usepackage{enumitem}                  %lepsi itemize
\usepackage{rotating}
\usepackage{multicol}   
\usepackage{makeidx}
\makeindex
% % % % % % % % % % % % % % % % % %
\setcounter{secnumdepth}{4}
\setcounter{tocdepth}{2}
\tolerance=1
\emergencystretch=\maxdimen
\hyphenpenalty=7000
\hbadness=7000
\let\cleardoublepage\clearpage
% % % % % % % % % % % % % % % %
\newcounter{thectrbox}
\newtheoremstyle{break}% name
{\topsep}%  Space above, empty = `usual value'
{\topsep}%   Space below
{}%  Body font
{}%          Indent amount (empty = no indent, \parindent = para indent)
{\bfseries}% Thm head font
{.}%         Punctuation after thm head
{ }%  Space after thm head: \newline = linebreak
{#1.#2~#3}%          Thm head spec
\theoremstyle{break}
\newtheorem{ctrbox}[thectrbox]{\thesection}
\newtheoremstyle{nbreak}% name
{\topsep}%  Space above, empty = `usual value'
{\topsep}%   Space below
{}%  Body font
{}%          Indent amount (empty = no indent, \parindent = para indent)
{\bfseries}% Thm head font
{.}%         Punctuation after thm head
{ }%  Space after thm head: \newline = linebreak
{#3}%          Thm head spec
\theoremstyle{nbreak}
\newtheorem{nctrbox}[]{}
% % % % % % % % % % % % % % % %
\setlist[itemize]{noitemsep,align=left,leftmargin=20pt,labelwidth=*}
\renewcommand{\labelitemi}{\textbullet}
\renewcommand{\labelitemii}{\textbullet}
\renewcommand{\labelitemiii}{\textbullet}
% % % % % % % % % % % % % % %
\renewcommand{\qedsymbol}{{\footnotesize $\blacksquare$}}
\makeatletter \renewenvironment{proof}[1][\proofname] {\par\pushQED{\qed}\normalfont\topsep6\p@\@plus6\p@\relax\trivlist\item[\hskip\labelsep\bfseries#1\@addpunct{.}]\ignorespaces}{\popQED\endtrivlist\@endpefalse} \makeatother
% % % % % % % % % % % % % % % % %
\renewcommand{\Im}{\mathrm{Im} \,} % image instead of imaginary part
\renewcommand{\tilde}{\widetilde}
\newcommand{\Arrow}{\! \! \! \! \rotatebox{90}{\makebox[0.02\textwidth]{\raisebox{-5pt}{\includegraphics[width=0.02\textwidth]{arrow.pdf}}}} \! \!\!}
% % % % % % % % % % % % % % % % % % Algebra befehle
\newcommand{\fix}{\mathrm{Fix}\,}
\newcommand{\sgn}{\mathrm{sgn}\,}
\newcommand{\id}{\mathrm{id}}
\newcommand{\Aut}{\mathrm{Aut}\,}
\newcommand{\coef}{\mathrm{coef}\,}
\newcommand{\Ker}{\mathrm{Ker}\,}
\newcommand{\ord}{\mathrm{ord}\,}
\newcommand{\Inv}{\mathrm{Inv}\,}
\newcommand{\xInv}[1]{#1\text{-}\mathrm{Inv}\,}
\newcommand{\nOrb}[1]{#1\text{-}\mathrm{Orb}\,}
\newcommand{\Orb}{\mathrm{Orb}\,}
\newcommand{\factor}[2]{\text{\raisebox{3pt}{$#1$}} \ \mathclap{\text{{\LARGE /}}} \ \text{\raisebox{-3pt}{$#2$}}}
\newcommand{\ifactor}[2]{\text{\raisebox{-3pt}{$#1$}} \ \mathclap{\text{{\LARGE \normalfont{\textbackslash}}}} \ \text{\raisebox{3pt}{$#2$}}}
\newcommand{\sub}[2]{\mathrm{Sub}_{#1}(#2)}
\newcommand{\syl}[2]{\mathrm{Syl}_{#1}(#2)}
\newcommand{\ntlneq}{\,\raisebox{1pt}{$\lhd$}\hspace{-6pt}\raisebox{-4pt}{$\neq$}\,}
\renewcommand{\epsilon}{\varepsilon} % schoeneres Epsilon
\renewcommand{\b}[1]{\boldsymbol{#1}}
% % % % % % % % % % % % % % % % % % % % Ersatz für mnsymbol
\newcommand{\cupdot}{\uplus}
\newcommand{\bigcupdot}{\biguplus}
\renewcommand{\div}{\mid}
\newcommand{\ndiv}{\nmid}
% % % % % % % % % % % % % % % % % % % %
\newcommand{\pot}[1]{\mathfrak{P}\left(#1\right)}
\makeatletter
\newcommand*{\defeq}{\mathrel{\rlap{%
			\raisebox{0.3ex}{$\m@th\cdot$}}%
		\raisebox{-0.3ex}{$\m@th\cdot$}}%
	=}
\newcommand*{\defiff}{\mathrel{\rlap{%
			\raisebox{0.3ex}{$\m@th\cdot$}}%
		\raisebox{-0.3ex}{$\m@th\cdot$}}%
	\Longleftrightarrow}
\newcommand*{\bdefiff}{\Longleftrightarrow\mathrel{\rlap{%
			\raisebox{0.3ex}{$\m@th\cdot$}}%
		\raisebox{-0.3ex}{$\m@th\cdot$}}%
}
\makeatother
% % % % % % % % % % % % % % % % % % % % % INDEX
\usepackage{makeidx}
\makeindex
\begin{document}

\begin{ctrbox}[Algorithmus]
	Step 1:   mache was \newline
	Step 2:   mache was \newline
	Step 3:   mach was und wenn das, dann geh zu S1 \newline
	Step 4:   Wenn das dann Ende, sonst gehe zu S1 und jetzt steht hier was ganz langes, was dann über 2 Zeilen geht und das soll nicht unter Step 4 stehen sondern genau unter dem Wenn \newline
\end{ctrbox}
\end{document}
Entschuldigt bitte die vielen Pakete, ich habe einfach fast alle genommen die auch in meiner Arbeit drin sind. Ich hoffe es kann mir jemand helfen und ich bin über jeden Hinweis dankbar !

Viele Grüße !

Nach oben