International keyboard settings
To make your keyboard have the accents working for Brazilian Portuguese on Debian-like operating systems in the proper way that Brazilians are used to, you need to add a second Keyboard Layout which uses the English (US, alternative international) language.
The dead keys are single quote, double quote, back-tick and tilde, typing any of these characters while using the international keyboard will result in an accent being applied to the next typed character - unless the next character is a space in which case the dead-key itself will be typed. For example single quote, double quote, back-tick and tilde respectively followed by the letter A results in á, ä, à and ã.
In Spanish, upside down question marks (¿) and exclamation marks (¡) are also required as they work like quotes that wrap around a sentence in Spanish. These characters are available on the international keyboard using right-alt + 1 and right-alt + /.
Mexican keyboard issues
We got a laptop from Mexico and it has a really strange keyboard, but worse still was that many of the keys didn't work in Linux, for example "@" is on the bottom-right of the "Q" key implying that Alt-Gr + Q should produce the "@" symbol, but it did not. Doing setxkbmap -query showed that it was set to latam.bz.us.us which is really messed up - it needs to just be latam.
To make matters more difficult, we need es, pt and en-intl keyboard layouts on the machine with the new latam setting applying only to the es layout, and furthermore the machine is used sometimes in Xorg and sometimes in Wayland!
In the end it was fixed by adding the following line to the end of ~/.profile.
setxkbmap -layout "latam,pt,us" -variant ",,intl"
Cedilla
An annoying hack is required to fix the cedilla, because the apostrophe+C yields a C with an accent instead of a Cedilla!. But note that if the hack is not installed, or it doesn't work on your distro, the cedilla is also available using right-alt + comma.
I had included some solutions for various Debian-based OS's here, but they were sub-optimal because they were always failing edge cases. But fortunately I recently found this cool fix-cedilla script by Marco Paganini that seems to work across most versions and flavours of Linux and in most programs whether they're shell, Gnome, Cinnamon, QT or whatever! Thanks Marco :-)
Note: This script stops dead keys from working in Telegram, but this can be fixed by commenting out the line that says QT_IM_MODULE=cedilla in the /etc/environment file.