这是对先前问题的回答:
使用 tcolorbox 绘制逼真的 Linux 命令 Shell 窗口
我希望能够创建类似这样的东西:
在大多数情况下我可以,但我无法重新定义提示和颜色等。所以基本上我希望能够定义多个every listing line
值或以某种方式在某些点任意重新定义它。例如:
爱丽丝
every listing line={\textcolor{magenta}{\small\ttfamily\bfseries{alice@localhost{\textcolor{black}:}\textcolor{cyan!60}{\url{~}}{\textcolor{black}\$ }}}}}
鲍勃
every listing line={\textcolor{cyan}{\small\ttfamily\bfseries{bob@remotehost{\textcolor{black}:}\textcolor{cyan!60}{\url{~}}{\textcolor{black}\$ }}}}}
姆韦
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[]{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{tcolorbox,url,xcolor}
\tcbuselibrary{skins,xparse,minted,listings}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newlength{\USER}
\setbox0\hbox{\small\ttfamily\bfseries ....}
\setlength{\USER}{\the\dimexpr\wd0+1pt}
\newcommand{\stdout}{\\\hspace*{-\USER}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newtcblisting{macos}{size=small,halign title=flush left,
title={\textcolor{red!62}{\huge\ttfamily\bfseries{$\bullet\,$}\textcolor{orange!52!yellow}{\huge\ttfamily\bfseries{$\bullet\,$}\textcolor{green!80!black}{\huge\ttfamily\bfseries{$\bullet\,$}}}}},
colback=white,colupper=black,colframe=gray!25,listing only,
listing options={style=tcblatex,language=sh,escapeinside=``},
every listing line={\textcolor{magenta}{\small\ttfamily\bfseries{alice@localhost{\textcolor{black}:}\textcolor{cyan!60}{\url{~}}{\textcolor{black}\# }}}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{macos}
ssh bob@remotehost`\stdout`bob@remotehost's`\ `password:`\stdout`Linux remotehost 2.6.32-5-686 #1 SMP Sun Sep 23 09:49:36 UTC 2012 i686`\stdout`You have mail.`\stdout`Last login: Wed Oct 16 01:12:35 2012 from localhost`\stdout``\stdout`bob@remotehost:~$ _
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{macos}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
答案1
您可以简单地即时更改提示。这可以通过三个命令完成
\StartAlice
,之后提示变成alice@localhost:~$
,\StartBob
,之后提示变成bob@localhost:~$
,\StartConsole
这导致没有提示。
梅威瑟:
\documentclass[]{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{tcolorbox,url,xcolor}
\tcbuselibrary{skins,xparse,minted,listings}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newtcblisting{macos}{size=small,halign title=flush left,
title={\textcolor{red!62}{\huge\ttfamily\bfseries{$\bullet\,$}\textcolor{orange!52!yellow}{\huge\ttfamily\bfseries{$\bullet\,$}\textcolor{green!80!black}{\huge\ttfamily\bfseries{$\bullet\,$}}}}},
colback=white,colupper=black,colframe=gray!25,listing only,
listing options={style=tcblatex,language=sh,escapeinside=``},
every listing line={\myprompt}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\myaliceprompt{\textcolor{magenta}{\small\ttfamily\bfseries{alice@localhost{\textcolor{black}:}\textcolor{cyan!60}{\url{~}}{\textcolor{black}\$ }}}}
\newcommand\mybobprompt{\textcolor{cyan}{\small\ttfamily\bfseries{bob@remotehost{\textcolor{black}:}\textcolor{cyan!60}{\url{~}}{\textcolor{black}\$ }}}}
\newcommand\myconsoleprompt{}
\newcommand{\StartBob}{\gdef\myprompt{\mybobprompt}}
\newcommand{\StartAlice}{\gdef\myprompt{\myaliceprompt}}
\newcommand{\StartConsole}{\gdef\myprompt{\myconsoleprompt}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\StartAlice
\begin{macos}
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
`\StartBob`
whoami`\StartConsole`
bob
\end{macos}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
可能更结构化的方法是仅定义\SU
要切换的命令。然后例如\SU{alice@localhost}
将提示更改为alice@localhost:~$
,并按\StartConsole
上述方式工作。
\documentclass[]{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{tcolorbox,url,xcolor}
\tcbuselibrary{skins,xparse,minted,listings}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newtcblisting{macos}{size=small,halign title=flush left,
title={\textcolor{red!62}{\huge\ttfamily\bfseries{$\bullet\,$}\textcolor{orange!52!yellow}{\huge\ttfamily\bfseries{$\bullet\,$}\textcolor{green!80!black}{\huge\ttfamily\bfseries{$\bullet\,$}}}}},
colback=white,colupper=black,colframe=gray!25,listing only,
listing options={style=tcblatex,language=sh,escapeinside=``},
every listing line={\myprompt}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\SU}[1]{\gdef\myprompt{\textcolor{magenta}{\small\ttfamily\bfseries{#1{\textcolor{black}:}\textcolor{cyan!60}{\url{~}}{\textcolor{black}\$ }}}}}
\newcommand{\StartConsole}{\gdef\myprompt{}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\SU{alice@localhost}
\begin{macos}
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@remotehost}`
whoami`\StartConsole`
bob
\end{macos}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}