2

On this site i found a very nice snippet of code to center a textblock of fixed width in my frame https://stackoverflow.com/a/23731757/4310112

The idea is that whenever the window size is change, the margins of the window are adjusted in a way that the text block keeps a pre defined fixed size. (The code is below.)

However it does not exactly what i expected. When i resize the window the text width changes as well. The will be some margins but not of the size intended. If i switch to another buffer and back the margins change to the correct size.

Something similar happens when i execute some command, for example C-c C-c in AucTex or just M-x C-g. Then all margins vanish and the text occupies the whole frame width. Again, switching buffers will restore the intended frame width.

;;Add left and right margins, when file is markdown or text.
(defun center-window (window) ""
  (let* ((current-extension (file-name-extension (or (buffer-file-name) "foo.unknown")))
         (max-text-width 80)
         (margin (max 0 (/ (- (window-width window) max-text-width) 2))))
    (if (and (not (string= current-extension "md"))
             (not (string= current-extension "tex")))
        ;; Do nothing if this isn't an .md or .tex file.
        ()
  (set-window-margins window margin margin))))

;; Adjust margins of all windows.
(defun center-windows () ""
  (walk-windows (lambda (window) (center-window window)) nil 1))

;; Listen to window changes.
(add-hook 'window-configuration-change-hook 'center-windows)
2

You might want to try centered-window-mode: https://github.com/anler/centered-window-mode

It centers the text of the window when there’s only one window in the frame. To enable it in all my LaTeX buffers I added the following hook

(add-hook 'LaTeX-mode-hook 'centered-window-mode)

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.