29

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}
0

1 Answer 1

33

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.

14
  • 2
    I did try to explain what I did but I am lacking fundamental information on what you do not understand in the explanation. This is a recurring problem, and there is a nice video explaining it. All I can say is that \` corresponds to a carriage return and \hspace*{-\Lind}` undoes the indent because \hspace generates some space but if its argument is negative it undoes it. I will be happy to explain differently if I know what part of my explanation you have trouble with.
    – user194703
    Commented Nov 25, 2019 at 22:04
  • 2
    @voices \setbox0\hbox{<something>} is a standard trick to measure the width of something. So I measure the width and store it in a dimension that I called \Lind for no special reason, you could use \IdontknowhowIwanttocallthislength instead. \the\dimexpr allows us to add lengths, which I used to increase the length by 1pt. The argument is just copied from every listing line because the font information does alter the width.
    – user194703
    Commented Nov 25, 2019 at 22:40
  • 4
    @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.)
    – user194703
    Commented Nov 26, 2019 at 2:40
  • 3
    @voices I added a version using pgf keys which simplifies things IMO.
    – user194703
    Commented Nov 27, 2019 at 5:16
  • 3
    @voices I added a way to change the prompt char to the lowest example.
    – user194703
    Commented Nov 28, 2019 at 19:30

You must log in to answer this question.

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