How can I set a dark theme in TeXworks?

I managed to figure this out by combining answers from two sources.

First, I found a post on the topic "Customize Editor Colors". The author of the post explains that you should create a custom style sheet mystyle.css using CSS mark-up, with the contents:

QTextEdit {
    background-color: black;  
    color: white;            /* sets the main text color */
}

When you run TeXworks from the command line, with the optional argument telling TeXworks to use that CSS file,

> texworks -stylesheet /pathto/mystyle.css

then the TeXworks editor shows white text on a black background:

enter image description here

Normally, I start TeXworks by clicking on a button on the Windows taskbar. I can edit the button/shortcut so that TeXworks loads the CSS file when it starts up as follows:

  1. Shift + right click on the button
  2. Click Properties
  3. Change

    "C:\Program Files\MiKTeX 2.9\miktex\bin\x64\miktex-texworks.exe"
    

    to

    "C:\Program Files\MiKTeX 2.9\miktex\bin\x64\miktex-texworks.exe" -stylesheet C:\Users\myusername\black-background.css
    
  4. Click OK


As the TeXworks manual says, TeXworks includes built-in support for Qt, and the application of Qt style sheets may be used to change the look of the TeXworks. By digging around a bit, I came up with this answer. First, set up your copy of TeXworks by pasting in the shortcut target

"C:\Program Files\MiKTeX 2.9\miktex\bin\x64\miktex-texworks.exe" -stylesheet "C:\Users\myusername\black-background.css"

as described in the answer of I Like To Code.

Paste in this code into your css file:

QWidget { /* everything, basically */
    background-color: #3A3A3A;
    color: white;
}

QPushButton { /* buttons */
    background-color: #646464;
    border-style: inset;
    border-width: 1.5pt;
    border-color: white;
    padding: 4px;
}

QPushButton:pressed { /* after you press the button */
    background-color: #818181;
}

QTableView {
   background-color: #424242;
   alternate-background-color: #424242;
   selection-background-color: #818181;
   font-color:#FFFFFF;
}

/* QComboBox refers to drop-down menus (such as when selecting pdfTeX, pdfLaTeX, XeTeX, etc.) */

QComboBox {
    border: 1px solid gray;
    padding: 1px 18px 1px 3px;
    border-radius: 4px;
}

QComboBox::drop-down { 
    subcontrol-origin: padding;
    subcontrol-position: top right;
    width: 15px;
    border-left-width: 1px;
    border-left-color: #EEEEEE;
    border-left-style: solid; /* just a single line */
}

QComboBox::down-arrow:on {
    top: 5px;
    left: 5px;
}

/* QMenuBar refers to the menu at the top (File, Edit, etc.)
   QMenu::item refers to the items under the menu
 */

QMenu::item:selected { /* when user selects item using mouse or keyboard */
    background-color: #909090;
}

QMenuBar::item:pressed,  QMenuBar::item:selected {
    background: #909090;
}

QTextEdit { /* text editor */
    background-color: black;  
    color: white; 
}

QScrollBar:vertical { /* vertical scroll bar */
    background: #424242;
    width: 10px;
    margin: 10px 0 10px 0;
}
QScrollBar::handle:vertical { /* the part of the scroll bar that gets dragged*/
    background: #AAAAAA;
    min-height: 20px;
}
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical { 
    /* this seems useless, but it is actually quite important- 
       it makes the scroll bar's background a uniform color instead
       of tiny dots, if it were removed. */
    background: none;
}

QScrollBar:horizontal { /* same thing, but horizontal */
    background: #424242;
    height: 10px;
    margin: 0 10px 0 10px;
}
QScrollBar::handle:horizontal { /* etc. */
    background: #AAAAAA;
    min-width: 20px;
}
QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal { 
    background: none;
}

QTabBar::tab { /* tabs that lead to different displays (such as those under "Preferences") */
    background: #3A3A3A;
    border: #909090;
    padding: 2px 4px 6px 8px;
    /* now I can't remember how this padding works, but removing it leads to ugly tab designs*/
}

QTabBar::tab:hover {
    background: #818181;
}

QTabBar::tab:selected {
    background: #909090;
}

This results in a style that even fixes the line numbering text color and various menus:

Dark-themed editor: Dark-themed PDF Viewer

Of course, you will want to change the colors of your syntax-patterns.txt (see here as to how to do it) to fit the black background. The code above does not completely fix TeXworks' editor style, but if you ask people with more experience with Qt Style Sheets, they may be able to help you better, or you can look in the Qt Style Sheets example page to customize it to your own preferences. The three main issues with this answer as it is now are that it does not completely change the color of the search menu, it does not show arrows in drop down menus, and the scroll bars seem to behave somewhat strangely. The images below illustrate these problems:

search problemdrop-down menu problem

scroll-bar problem 1scroll-bar problem 2

Hopefully, this TeXworks style will suffice, even with its imperfections.

Tags:

Texworks