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

3
  • 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. Commented Dec 1, 2016 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
    Commented Dec 1, 2016 at 14:07
  • I added an answer to show one possibility. Commented Dec 2, 2016 at 9:39

1 Answer 1

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

2
  • 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
    Commented Dec 5, 2016 at 20:01
  • found it, if use comment={\begin{tabular}{‎l}‎#1\end{‎tabular}‎}, don't necessary add tabular env to all.
    – niksirat
    Commented Dec 5, 2016 at 20:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .