0

i have 3 questions.

  1. how can justify low part of below code center and flushleft?
  2. how can customize keyword of maple in low part automatically?
  3. how auto indent code automatically?

code is:

‎\documentclass{article}‎

‎\usepackage{tcolorbox}‎
‎\usepackage{amsmath,amsfonts,amssymb,amsthm}‎

‎\tcbuselibrary{listings,skins,theorems}‎

‎\lstdefinelanguage{Maple}{‎
            ‎morekeywords={‎
            ‎and,assuming,break,by,catch,description,do,done‎,
            ‎elif,else,end,error,export,fi,finally,for,from,global,if‎,
            ‎implies,in,intersect,local,minus,mod,module,next,not,od‎,
            ‎option,options,or,proc,quit,read,return,save,stop,subset,then‎,
            ‎to,try,union,use,uses,while,xor‎
            ‎}‎,
            ‎sensitive=true‎,
            ‎keywordstyle=\color{black}‎,
            ‎morecomment=[l][\color{green!50!black}]\#‎,
            ‎morestring=[b]"‎,
            ‎morestring=[d]‎"
            ‎aboveskip={0pt}‎,
            ‎belowskip={0pt}‎,
            ‎}[keywords,comments,strings]‎

‎\newtcblisting{code}[1]{‎
             ‎skin=bicolor‎,
             ‎colback=white‎,
             ‎colbacklower=white‎,
             ‎colupper=red!40!black‎,
             ‎collower=black‎,
             ‎listing options={language={Maple}}‎,
             ‎fontupper=\ttfamily\bfseries‎,
             ‎fontlower=\itshape‎,
%             ‎fontlower=\rm\bf‎,
%            ‎math lower‎,
             ‎boxrule=0mm‎,
             ‎top=0mm,bottom=0mm,middle=0mm‎,
             ‎center lower‎,
%             ‎flushleft lower‎,
             ‎nobeforeafter‎, 
           ‎listing and comment‎,
           ‎comment={ #1}‎,
           ‎every listing line={\textcolor{red!40!black}{\ttfamily>‎ }}
           }

‎\begin{document}‎



‎\begin{code}{‎
‎Fib‎‎ :‎= ‎proc‎(‎‎n::nonnegint)\\‎
option remember; system;\\‎
if $n < 2$ then $n$ else ‎Fib‎$‎(n‎ - ‎1)‎‎$‎ + ‎Fib‎$‎(n‎ - ‎2)$ end if\\‎
‎end proc‎
}
‎Fib‎ :‎= proc( n‎ :: ‎nonnegint‎ )
‎option remember‎, ‎system;‎
‎if n<2 then‎
‎n‎
‎else‎
‎Fib(n-1)‎ + ‎Fib(n-2)‎
‎end if;‎
‎end proc;‎
‎\end{code}‎


‎\end{document}

output is: enter image description here

i want: enter image description here

  • I do not know a way to format and indent pseudocode with LaTeX automatically. And I'm very sure that tcolorbox cannot do such things - code formatting is done with listings or minted when using tcolorbox. The pseudocode could be formatted manually with packages like algorithm2e, but not automatically. – Thomas F. Sturm Dec 1 '16 at 13:38
  • @ThomasF.Sturm, thanks for answer, i want a way to justify center, and flushleft the result. when use "‎center lower‎", output isn't what i want. – niksirat Dec 1 '16 at 14:07
  • I added an answer to show one possibility. – Thomas F. Sturm Dec 2 '16 at 9:39
2

I do not know a way to format and indent pseudocode with LaTeX automatically. And I'm very sure that tcolorbox cannot do such things - code formatting is done with listings or minted when using tcolorbox. The pseudocode could be formatted manually with packages like algorithm2e, but not automatically.

To get a left justified block which is centered, the content has to be put into some left justified box which is centered afterwards.

One way to achieve the desired output without additional packages is to put the pseudocode into a table and format the keyword by an easy macro like \mykw:

\documentclass{article}

\usepackage{tcolorbox}
\usepackage{amsmath,amsfonts,amssymb,amsthm}

\tcbuselibrary{listings,skins,theorems}

\lstdefinelanguage{Maple}{
            morekeywords={
            and,assuming,break,by,catch,description,do,done,
            elif,else,end,error,export,fi,finally,for,from,global,if,
            implies,in,intersect,local,minus,mod,module,next,not,od,
            option,options,or,proc,quit,read,return,save,stop,subset,then,
            to,try,union,use,uses,while,xor
            },
            sensitive=true,
            keywordstyle=\color{black},
            morecomment=[l][\color{green!50!black}]\#,
            morestring=[b]",
            morestring=[d]"
            aboveskip={0pt},
            belowskip={0pt},
            }[keywords,comments,strings]

\newtcblisting{code}[1]{
             skin=bicolor,
             colback=white,
             colbacklower=white,
             colupper=red!40!black,
             collower=black,
             listing options={language={Maple}},
             fontupper=\ttfamily\bfseries,
             fontlower=\itshape,
%             fontlower=\rm\bf,
%            math lower,
             boxrule=0mm,
             top=0mm,bottom=0mm,middle=0mm,
             center lower,
%             flushleft lower,
             nobeforeafter,
           listing and comment,
           comment={ #1},
           every listing line={\textcolor{red!40!black}{\ttfamily> }}
           }

\begin{document}

\newcommand{\mykw}[1]{\textbf{\upshape #1}}

\begin{code}{
\begin{tabular}{l}
Fib := \mykw{proc}(n::nonnegint)\\
\mykw{option} remember; system;\\
\hspace{4mm}\mykw{if} $n < 2$ \mykw{then} $n$ \mykw{else} Fib$(n - 1)$ + Fib$(n - 2)$ \mykw{end if}\\
\mykw{end proc}
\end{tabular}
}
Fib := proc( n :: nonnegint )
option remember, system;
if n<2 then
n
else
Fib(n-1) + Fib(n-2)
end if;
end proc;
\end{code}


\end{document}

This gives:

enter image description here

  • Is there a way to do not repeat "\begin{code}{\begin{tabular}{l}" and "\end{tabular}}" ? i mean, add \begin{tabular}{l} and \end{tabular} automatically? – niksirat Dec 5 '16 at 20:01
  • found it, if use comment={\begin{tabular}{‎l}‎#1\end{‎tabular}‎}, don't necessary add tabular env to all. – niksirat Dec 5 '16 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.