# Customizing TLG Visual Formats

#### Yolanda Zhou ([`yolandazzz13`])

#### 2025-09-25

# Overview

This guide will walk you through utilizing functions for styling your tables. We have implemented a base formatting function and several simple theme wrappers, and you can easily switch between different visual styles. For a complete list of implementations we have for you, please go to `R/ft_formats.R`.

### Pattern A: autoslider Base Theme

The `autoslider_format` function is a great example of a base theme. It defines a “zebra-striped” layout and accepts many arguments for colors and fonts, giving you full control.

    autoslider_format <- function(ft,
                                  odd_header = "#0EAED5",
                                  odd_body = "#EBF5FA",
                                  even_body = "#D0E4F2",
                                  ...) {
      ft %>%
        flextable::theme_zebra(
          odd_header = odd_header,
          odd_body = odd_body,
          even_header = odd_header, # Use same color for even header
          even_body = even_body
        )
      # ... other general formatting ...
    }

### Pattern B: Simple Theme Wrappers

Wrapper functions call the base theme with predefined colors. This makes it easy to switch between styles.

    blue_format <- function(ft, ...) {
      # Calls the base function with specific blue colors
      ft %>% autoslider_format(
        odd_header = "#0B41CD",
        odd_body = "#1482FA",
        even_body = "#BDE3FF",
        ...
      )
    }

### Pattern C: Standalone Formats

For unique tables, you can create a standalone function that doesn’t use the base theme at all, like `black_format_tb.`

    black_format_tb <- function(ft, ...) {
      ft %>%
        flextable::theme_booktabs() %>%
        flextable::color(color = "blue", part = "header")
      # ... other specific formatting ...
    }

## How to Apply Your Custom Formats

If you have special demands that require customized implementation of TLG formats, feel free to implement them on your own. The most direct way to use and test these functions is to apply them to a `flextable` object after it has been created.

The workflow involves three steps:

- Generate your list of outputs.

- Convert a specific output into a `flextable` object.

- Pass the resulting `flextable` object directly to your formatting function.

#### Example: Visualize the output of your format implementation

    outputs <- spec_file %>%
      read_spec() %>%
      filter_spec(program %in% c("t_dm_slide")) %>%
      generate_outputs(datasets = my_data) %>%
      decorate_outputs()

    # Step 2: Convert a specific output to a flextable object
    dm_table <- to_flextable(outputs$t_dm_slide_ITT)
    # The to_flextable() function returns a list, so we get the first element
    dm_flextable <- dm_table[[1]]$ft

    # Step 3: Apply your formatting functions
    black_ft <- my_own_format(dm_flextable)
    print(black_ft)