18

I'm trying to make these little imitation command shell window/box/things. The problem is I can't skip lines. The prompt (aka. $PS1) appears on every line; there's no room for the command output. Any ideas? Thanks.

PS: I'm using tcolorbox to draw the boxes. I usually use minted for code formatting (including elsewhere in the same document), but these boxes use listings, like the examples in the manual.


How it is:

a) enter image description here LaTeX
b) enter image description here LaTeX


How it should be:

enter image description here Inkscape


Code:

\documentclass[]{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{tcolorbox,url}
\tcbuselibrary{skins,xparse,minted,listings}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%% This is where it's defined %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newtcblisting{ubuntu}{colback=violet!50!black,colupper=white,
colframe=gray!65!black,listing only,listing options={style=tcblatex,language=sh},
title={\textcolor{orange}{\Huge{$\bullet$}}{\textcolor{gray}{\Huge{$\bullet\bullet$}}}},
every listing line={\textcolor{lime}{\small\ttfamily\bfseries
root@ubuntu{\textcolor{white}:}\textcolor{cyan!60}{\url{~}}{\textcolor{white}\#}}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%% This is where it's drawn %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{ubuntu}
whoami \\ root
id \\ uid=0(root) gid=0(root) groups=0(root)
hostname \\ ubuntu
_
\end{ubuntu}    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}
22

You can escape inside a listing. I defined a command that brings you in a new line without adding root@ubuntu:~#. (I could only run the code after Tiuri's edit.)

\documentclass[]{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{tcolorbox,url}
\tcbuselibrary{skins,xparse,listings}

\begin{document}

\newtcblisting{ubuntu}{colback=violet!50!black,
colupper=white,colframe=gray!65!black,listing only,
listing options={style=tcblatex,language=sh,escapeinside=||,},
title={\textcolor{orange}{\Huge{$\bullet$}}{\textcolor{gray}{\Huge{$\bullet\bullet$}}}},
every listing line={\textcolor{lime}{\small\ttfamily\bfseries root@ubuntu{\textcolor{white}:}\textcolor{cyan!60}{\url{~}}{\textcolor{white}\#} }}}
\newlength{\Lind}
\setbox0\hbox{\small\ttfamily\bfseries root@}
\setlength{\Lind}{\the\dimexpr\wd0+1pt}
\newcommand{\LR}{\\\hspace*{-\Lind}}
\begin{ubuntu}
 whoami |\LR| root
 id |\LR| uid=0(root) gid=0(root) groups=0(root)
 hostname |\LR| ubuntu
 _
\end{ubuntu}

\end{document}

enter image description here

ADDENDUM/IMPROVEMENT. After answering this follow-up question I feel that the above is unnecessarily complicated. One can just define a command, \StartConsole, that removes the prompt, and a command \SU that installs a certain prompt. This command can be used e.g. as in

\SU{user=bob,host=remotehost,color=cyan}

which changes the prompt to

 bob@remotehost:~# 

Even better, one can install styles for users. For instance,

\pgfkeys{/ubuntu/.cd,bob/.style={user=bob,host=remotehost,color=cyan}}

followed by

\SU{bob}

will achieve the same.

\documentclass[]{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{tcolorbox,url}
\tcbuselibrary{skins,xparse,listings}

\begin{document}

\newtcblisting{ubuntu}{colback=violet!50!black,
colupper=white,colframe=gray!65!black,listing only,
listing options={style=tcblatex,language=sh,escapeinside=``,},
title={\textcolor{orange}{\Huge{$\bullet$}}{\textcolor{gray}{\Huge{$\bullet\bullet$}}}},
every listing line={\MyUbuntuPrompt}}
\pgfkeys{/ubuntu/.cd,
user/.code={\gdef\MyUbuntuUser{#1}},user={},
host/.code={\gdef\MyUbuntuHost{#1}},host={},
color/.code={\gdef\MyUbuntuColor{#1}},color=white,
prompt char/.code={\gdef\MyUbuntuPromptChar{#1}},prompt char=\#,
root/.style={user=root,host=ubuntu,color=lime,prompt char=\#},
bob/.style={user=bob,host=remotehost,color=cyan},
}
\newcommand{\SU}[1]{\pgfkeys{/ubuntu/.cd,#1}%
\gdef\MyUbuntuPrompt{\textcolor{\MyUbuntuColor}{\small\ttfamily\bfseries \MyUbuntuUser@\MyUbuntuHost{\textcolor{white}:}\textcolor{cyan!60}{\url{~}}{\textcolor{white}\MyUbuntuPromptChar} }}}
\newcommand{\StartConsole}{\gdef\MyUbuntuPrompt{}}

\SU{user=root,host=ubuntu,color=lime}
\begin{ubuntu}
whoami `\StartConsole`
root `\SU{root}` 
id `\StartConsole` 
uid=0(root) gid=0(root) groups=0(root)`\SU{root}`
hostname `\StartConsole`
ubuntu`\SU{root}`
ssh bob@remotehost`\StartConsole`
bob@remotehost's`\ `password:
Linux remotehost 2.6.32-5-686 #1 SMP Sun Sep 23 09:49:36 UTC 2012 i686
You have mail.
Last login: Wed Oct 16 01:12:35 2012 from localhost
`\SU{bob}`
whoami`\StartConsole`
bob`\SU{bob}`
_
\end{ubuntu}

\end{document}

enter image description here

User names with special characters also work without problems. The fat underscore is borrowed from this answer.

\documentclass[]{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{tcolorbox,url,tikzlings}
\tcbuselibrary{skins,xparse,listings}

\begin{document}

\newtcblisting{ubuntu}{colback=violet!50!black,
colupper=white,colframe=gray!65!black,listing only,
listing options={style=tcblatex,language=sh,escapeinside=``,},
title={\textcolor{orange}{\Huge{$\bullet$}}{\textcolor{gray}{\Huge{$\bullet\bullet$}}}},
every listing line={\MyUbuntuPrompt},enhanced,
overlay={\begin{tcbinvclipframe}
\penguin[shift={([xshift=2cm,yshift=-0.8cm]frame.north)}]
\end{tcbinvclipframe}}}
\pgfkeys{/ubuntu/.cd,
user/.code={\gdef\MyUbuntuUser{#1}},user={},
host/.code={\gdef\MyUbuntuHost{#1}},host={},
color/.code={\gdef\MyUbuntuColor{#1}},color=white,
prompt char/.code={\gdef\MyUbuntuPromptChar{#1}},prompt char=\#,
root/.style={user=root,host=ubuntu,color=lime,prompt char=\#},
bob/.style={user=bob,host=remotehost,color=cyan},
cat/.style={user={schroedingers\char`_cat},host=burrow,color=magenta,prompt char=\$}
}
\newcommand{\SU}[1]{\pgfkeys{/ubuntu/.cd,#1}%
\gdef\MyUbuntuPrompt{\textcolor{\MyUbuntuColor}{\small\ttfamily\bfseries \MyUbuntuUser@\MyUbuntuHost{\textcolor{white}:}\textcolor{cyan!60}{\url{~}}{\textcolor{white}\MyUbuntuPromptChar} }}}
\newcommand{\StartConsole}{\gdef\MyUbuntuPrompt{}}

\SU{user=root,host=ubuntu,color=lime}
\begin{ubuntu}
whoami `\StartConsole`
root `\SU{root}` 
id `\StartConsole` 
uid=0(root) gid=0(root) groups=0(root)`\SU{root}`
hostname `\StartConsole`
ubuntu`\SU{root}`
ssh cat@burrow`\StartConsole`
bob@remotehost's`\ `password:
Linux remotehost 2.6.32-5-686 #1 SMP Sun Sep 23 09:49:36 UTC 2012 i686
You have mail.
Last login: Wed Oct 16 01:12:35 2012 from localhost
`\SU{cat}`
whoami`\StartConsole`
schroedingers_cat`\SU{cat}`
_
\end{ubuntu}

\end{document}

enter image description here

The penguin must have seen the many occurrences of the word "Ubuntu" here.

  • Can you explain what you did? I see you added escapeinside=||, which is obvious, but i've never used these guys: \newlength{\Lind} \setbox0\hbox{\small\ttfamily\bfseries root@} \setlength{\Lind}{\the\dimexpr\wd0+1pt} \newcommand{\LR}{\\\hspace*{-\Lind}}. I mean, I can see what you've done and why in a broad sense (simply escaping \newline & \\ seems to leave indents & extra whitespace), but I don't understand how it works. Rather than just copying you blindly, it would be valuable to know the details so I can learn to solve this kind of problem on my own next time. – voices Nov 25 '19 at 21:55
  • 2
    @voices At some level, these are very good analogies except for \hspace, which inserts a space that is given by its argument. (I am only on my cell phone now.) – Schrödinger's cat Nov 26 '19 at 2:40
  • 1
    @voices Yes. There is a simple trick: you can just change the prompt on the fly. I wrote an answer for that. – Schrödinger's cat Nov 27 '19 at 4:09
  • 1
    @voices I added a version using pgf keys which simplifies things IMO. – Schrödinger's cat Nov 27 '19 at 5:16
  • 1
    @voices I added a way to change the prompt char to the lowest example. – Schrödinger's cat Nov 28 '19 at 19:30

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.