tcbuselibrary:重新定義“每個清單行”

tcbuselibrary:重新定義“每個清單行”

這是先前問題的後續:
使用 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}

相關內容